#include <bits/stdc++.h>   using namespace std;   const int MX = 1e5 + 5;   int n;   int M[9][2] = {{0, 0},  {-1, -1}, {1, 1},  {0, 1}, {1, 0},                  {0, -1}, {-1, 0},  {1, -1}, {-1, 1}};   int compute(vector<int> Arr, int A0, int A1, int val) {     Arr[0] = A0;     Arr[1] = A1;     int d = Arr[1] - Arr[0];     int ans = val;     for (int i = 2; i < n; i++) {       if (d + Arr[i - 1] != Arr[i]) {         if (d + Arr[i - 1] != Arr[i] + 1) {           if (d + Arr[i - 1] != Arr[i] - 1) {             return -1;           } else {             Arr[i]--;             ans++;           }         } else {           Arr[i]++;           ans++;         }       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int val;     cin >> n;     vector<int> A;     for (int i = 0; i < n; cin >> val, A.push_back(val), i++)       ;     int ans = -1;     for (int i = 0; i < 9; i++) {       int num =           compute(A, A[0] + M[i][0], A[1] + M[i][1], abs(M[i][0]) + abs(M[i][1]));       if (num != -1) {         if (ans == -1)           ans = num;         else           ans = min(ans, num);       }     }     cout << ans << endl;     return 0;   }   