#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long cdiff, d, diff, rem, c, n, m, i, k = 0, j, flag = 0, x,                                                minstep = 10000000000;     cin >> n;     long long b[n];     for (i = 0; i < n; i++) cin >> b[i];     if (n < 3)       cout <<  0 ;     else {       cdiff = b[0] - b[n - 1];       diff = abs(b[0] - b[n - 1]);       rem = diff % (n - 1);       d = cdiff / (n - 1);       if (rem > 2 && rem < n - 3)         cout << -1 << endl;       else {         if (rem == 2 || rem == 0 || rem == 1) {           c = 0;           flag = 0;           for (i = 0; i < n; i++) {             if (b[i] - (b[0] - (i * d)) > 1 || b[i] - (b[0] - (i * d)) < -1) {               flag = 1;               break;             } else if (b[i] - (b[0] - (i * d)) != 0) {               c++;             }           }           if (flag == 0) minstep = min(c, minstep);         }         if (rem == 2 || rem == 0 || rem == 1) {           c = 0;           flag = 0;           for (i = 0; i < n; i++) {             if (b[i] - (b[0] + 1 - (i * d)) > 1 ||                 b[i] - (b[0] + 1 - (i * d)) < -1) {               flag = 1;               break;             } else if (b[i] - (b[0] + 1 - (i * d)) != 0) {               c++;             }           }           if (flag == 0) minstep = min(c, minstep);         }         if (rem == 2 || rem == 0 || rem == 1) {           c = 0;           flag = 0;           for (i = 0; i < n; i++) {             if (b[i] - (b[0] - 1 - (i * d)) > 1 ||                 b[i] - (b[0] - 1 - (i * d)) < -1) {               flag = 1;               break;             } else if (b[i] - (b[0] - 1 - (i * d)) != 0) {               c++;             }           }           if (flag == 0) minstep = min(c, minstep);         }         if (cdiff > 0)           x = 1;         else if (cdiff < 0)           x = -1;         else           x = 0;         d = (cdiff / (n - 1)) + x;         if (rem == n - 2 || rem == n - 3) {           c = 0;           flag = 0;           for (i = 0; i < n; i++) {             if (b[i] - (b[0] - (i * d)) > 1 || b[i] - (b[0] - (i * d)) < -1) {               flag = 1;               break;             } else if (b[i] - (b[0] - (i * d)) != 0) {               c++;             }           }           if (flag == 0) minstep = min(c, minstep);         }         if (rem == n - 3 || rem == n - 2) {           c = 0;           flag = 0;           for (i = 0; i < n; i++) {             if (b[i] - (b[0] + 1 - (i * d)) > 1 ||                 b[i] - (b[0] + 1 - (i * d)) < -1) {               flag = 1;               break;             } else if (b[i] - (b[0] + 1 - (i * d)) != 0) {               c++;             }           }           if (flag == 0) minstep = min(c, minstep);         }         if (rem == n - 3 || rem == n - 2) {           c = 0;           flag = 0;           for (i = 0; i < n; i++) {             if (b[i] - (b[0] - 1 - (i * d)) > 1 ||                 b[i] - (b[0] - 1 - (i * d)) < -1) {               flag = 1;               break;             } else if (b[i] - (b[0] - 1 - (i * d)) != 0) {               c++;             }           }           if (flag == 0) minstep = min(c, minstep);         }         if (minstep == 10000000000)           cout << -1 << endl;         else           cout << minstep << endl;       }     }     return 0;   }