#include <bits/stdc++.h>
using namespace std;
int findAns(vector<int> arr) {
  int diff = arr[1] - arr[0], totalOperations = 0;
  for (int i = 2; i < arr.size(); i++) {
    if (abs(arr[i] - arr[i - 1] - diff) > 1)
      return -1;
    else {
      totalOperations += abs(arr[i] - arr[i - 1] - diff);
      arr[i] = arr[i - 1] + diff;
    }
  }
  return totalOperations;
}
int main() {
  int size;
  cin >> size;
  vector<int> arr(size);
  for (int i = 0; i < size; i++) cin >> arr[i];
  if (size <= 2) {
    cout << 0 << endl;
    return 0;
  }
  int lim = 1000000000, minOperations = lim;
  int first = arr[0], second = arr[1];
  for (int i = 1; i >= -1; i--)
    for (int j = 1; j >= -1; j--) {
      arr[0] = first + i;
      arr[1] = second + j;
      int currAns = findAns(arr);
      if (currAns != -1)
        minOperations = min(minOperations, currAns + abs(i) + abs(j));
    }
  if (minOperations != lim)
    cout << minOperations << endl;
  else
    cout << "-1" << endl;
  return 0;
}
