#include #define endl '\n' using namespace std; template inline void chkmax(T &x, const T1 &y) { if (x < y) x = y; } template inline void chkmin(T &x, const T1 &y) { if (x > y) x = y; } const string TASK_NAME = "negsort"; const bool DEBUG = false; int n; int A[100000]; void read() { cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; } int get_number_of_zeros(int &pos) { int cnt = 0; for (int i = 0; i < n; i++) if (A[i] == 0) cnt++, pos = i; return cnt; } bool have_negative(int pos) { for (int i = 0; i < pos; i++) if (A[i] < 0) return true; return false; } void solve() { int ans = 0; int pos = -1; int cnt = get_number_of_zeros(pos); if (cnt > 1) { cout << -1 << endl; return; } else if (cnt == 0) { int len = 1; for (int i = 0; i < n - 1; i++) { if (A[i] > A[i + 1]) { if ((A[i] * -1) > A[i + 1]) { cout << -1 << endl; return; } else { ans += len; len = 1; } } else if (A[i] == A[i + 1]) len++; else len = 1; } cout << ans << endl; } else { if (have_negative(pos)) { for (int i = 0; i < pos; i++) if (A[i] > 0) A[i] *= -1, ans++; for (int i = 0; i < pos - 1; i++) if (A[i] > A[i + 1]) { cout << -1 << endl; return; } } for (int i = pos + 1; i < n; i++) if (A[i] < 0) A[i] *= -1, ans++; for (int i = pos + 1; i < n - 1; i++) if (A[i] > A[i + 1]) { cout << -1 << endl; return; } cout << ans << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); if (!DEBUG) { freopen((TASK_NAME + ".in").c_str(), "rt", stdin); freopen((TASK_NAME + ".out").c_str(), "wt", stdout); } read(); solve(); return EXIT_SUCCESS; }