#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> arr;
  if (n < 3)
    cout << 0;
  else {
    for (long long i = 0; i < n; i++) {
      long long d;
      cin >> d;
      arr.push_back(d);
    }
    vector<long long> ans1 = {arr[0], arr[0] + 1, arr[0] - 1};
    vector<long long> ans2 = {arr[1], arr[1] + 1, arr[1] - 1};
    long long ans = INT_MAX;
    for (long long l = 0; l < ans1.size(); l++) {
      for (long long k = 0; k < ans2.size(); k++) {
        long long j = 2;
        long long diff = -ans1[l] + ans2[k];
        long long flag = 1;
        long long t = 0;
        if (l != 0) t++;
        if (k != 0) t++;
        while (j < n) {
          if (arr[j] - ans1[l] == j * diff)
            ;
          else if (arr[j] - ans1[l] + 1 == j * diff or
                   arr[j] - ans1[l] - 1 == j * diff)
            t++;
          else {
            flag = 0;
            break;
          }
          j++;
        }
        if (flag == 1) ans = min(ans, t);
      }
    }
    if (ans != INT_MAX)
      cout << ans;
    else
      cout << -1;
  }
  return 0;
}
