#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MAXN = 5 + 100000;
int n, arr[MAXN];
ll cp[MAXN];
pair<bool, int> solve(ll dif) {
  int ans = 0;
  for (int i = 2; i < n; ++i) {
    ll desired = cp[i - 1] + dif;
    if (abs(cp[i] - desired) > 1) return {false, 0};
    ans += (int)abs(cp[i] - desired);
    cp[i] = desired;
  }
  return {true, ans};
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  if (n == 1) {
    cout << 0 << '\n';
    return 0;
  }
  for (int i = (int)0; i < (int)n; ++i) cin >> arr[i];
  bool any = false;
  int ans = MAXN;
  for (int x = arr[0] - 1; x <= arr[0] + 1; ++x) {
    for (int y = arr[1] - 1; y <= arr[1] + 1; ++y) {
      for (int i = (int)0; i < (int)n; ++i) cp[i] = arr[i];
      cp[0] = x;
      cp[1] = y;
      auto p = solve(y - x);
      int cost = abs(x - arr[0]) + abs(y - arr[1]) + p.second;
      if (p.first) {
        any = true;
        ans = min(ans, cost);
      }
    }
  }
  if (any)
    cout << ans << '\n';
  else
    cout << -1 << '\n';
  return 0;
}
