#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  if (n <= 2) {
    cout << 0;
    exit(0);
  }
  vector<long long int> b(n);
  for (int i = 0; i < n; i++) cin >> b[i];
  vector<long long int> brute{-1, 0, 1};
  long long int a1, a2, ans = INT_MAX;
  bool exist = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      a1 = b[0] + brute[i], a2 = b[1] + brute[j];
      long long int d = a2 - a1;
      long long int k = 2, cnt = 0;
      if (a1 != b[0]) cnt++;
      if (a2 != b[1]) cnt++;
      for (; k < n; k++) {
        if (abs(b[k] - (a1 + k * d)) > 1)
          break;
        else
          cnt += max((long long int)0, abs(b[k] - (a1 + k * d)));
      }
      if (k == n) {
        exist = 1;
        ans = min(ans, cnt);
      }
    }
  }
  if (exist)
    cout << ans;
  else
    cout << -1;
  return 0;
}
