#include <bits/stdc++.h>
using namespace std;
int db;
vector<int> sor;
bool ellenorzes() {
  vector<int> ell = sor;
  for (int i = 2; i < sor.size(); i++) {
    if (sor[i] - sor[i - 1] + 1 == sor[i - 1] - sor[i - 2]) {
      sor[i]++;
      db++;
      continue;
    }
    if (sor[i] - sor[i - 1] == sor[i - 1] - sor[i - 2]) {
      continue;
    }
    if (sor[i] - sor[i - 1] - 1 == sor[i - 1] - sor[i - 2]) {
      sor[i]--;
      db++;
      continue;
    }
    sor = ell;
    return false;
  }
  sor = ell;
  return true;
}
int main() {
  int n;
  cin >> n;
  sor.resize(n);
  set<int> mo;
  for (int i = 0; i < n; i++) cin >> sor[i];
  if (n == 1 || n == 2) {
    cout << 0;
    return 0;
  }
  sor[0]++;
  sor[1]++;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      db = 0;
      if (i == 0 || i == 2) db++;
      if (j == 0 || j == 2) db++;
      if (ellenorzes()) {
        mo.insert(db);
      }
      sor[1]--;
    }
    sor[1] += 3;
    sor[0]--;
  }
  if (mo.size() == 0)
    cout << -1;
  else
    cout << *mo.begin();
  return 0;
}
