#include <bits/stdc++.h>
using namespace std;
int n;
int b[100005];
int a[100005];
int help[100005];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &b[i]);
  if (n <= 2) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < n - 1; i++) a[i] = b[i + 1] - b[i];
  int maxvalue = -0x3f3f3f3f;
  int minvalue = 0x3f3f3f3f;
  for (int i = 0; i < n - 1; i++) {
    maxvalue = max(maxvalue, a[i]);
    minvalue = min(minvalue, a[i]);
  }
  if (maxvalue - minvalue > 4)
    printf("-1\n");
  else {
    bool flag;
    int ans = 0x3f3f3f3f;
    int res;
    for (int i = -1; i <= 1; i++) {
      for (int j = minvalue; j <= maxvalue; j++) {
        flag = true;
        help[0] = b[0] + i;
        for (int k = 0; k < n; k++) {
          if (abs(help[0] + k * j - b[k]) > 1) {
            flag = false;
            break;
          }
        }
        if (flag) {
          res = 0;
          for (int k = 0; k < n; k++) res += abs(help[0] + k * j - b[k]);
          ans = min(ans, res);
        }
      }
    }
    if (ans == 0x3f3f3f3f)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}
