#include #define filein(str) freopen(str, "r", stdin) #define fileout(str) freopen(str, "w", stdout); using namespace std; int nums[1000005]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); filein("negsort.in"); fileout("negsort.out"); int n; cin >> n; for(int i = 0; i < n; i++) cin >> nums[i]; int i = 0; long long ans1 = 0; bool res = false; while(i < n - 1 and abs(nums[i]) <= abs(nums[i + 1])) { if(nums[i] < 0) ans1++; i++; } if(i == n - 1) { if(nums[i] < 0) ans1++; res = true; } i = 0; long long ans = 0; while(i < n - 1 and abs(nums[i]) >= abs(nums[i + 1])) { if(nums[i] > 0) ans++; i++; } int j = i, curRes = 0, bestRes = 0; while(i > 0 and abs(nums[i]) == abs(nums[i - 1])) { if(nums[i] > 0) curRes++; else curRes--; if(curRes > bestRes) { bestRes = curRes; j = i; } i--; } ans -= bestRes; bool flag = false; if(j != n - 1) i = j - 1; else i = j, flag = true; if(i != n - 1) i++; else flag = true; while(i < n - 1 and abs(nums[i]) <= abs(nums[i + 1])) { if(nums[i] < 0) ans++; i++; } if(i != n - 1) cout << -1 << endl; else { if(nums[i] < 0 and !flag) ans++; if(res) cout << min(ans1, ans) << endl; else cout << ans << endl; } } /* 5 1 1 1 -1 -1 11 2 2 -1 -1 1 1 1 1 -1 1 1 */