#define CRT_SECURE_NO_WARNINGS
#define _CRT_DISABLE_PERFCRIT_LOCKS
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <ctime>
#include <sstream>
#include <cmath>
#include <stack>
#include <map>
#include <iterator>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef double dd;
typedef long double ld;
typedef unsigned long long ull;

#define all(x) x.begin(), x.end()
#define pb push_back

const int MOD = 998244353;
const int INF = 2e9;
const int maxN = 5e5 + 5;
const ll INFl = 2e14 + 1;
const ld eps = (ld)1e-6;
double getTime() { return clock() / (double)CLOCKS_PER_SEC; };

vector<pair<int, int>> ev = {  {1, -1},{1, 1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, 0}  };

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n; cin >> n;
	vector<int>a(n), b(n);
	for (int i = 0; i < n; i++) cin >> a[i], b[i] = a[i];
	if (n <= 2) {
		cout << 0;
		return 0;
	}
	int ans = 2e9;
	for (int i = 0; i < 9; i++) {
		int cnt = 0;
		a[0] += ev[i].first;
		a[1] += ev[i].second;
		if (abs(ev[i].first)) cnt++;
		if (abs(ev[i].second)) cnt++;
		int razn = abs(a[0] - a[1]);
		bool ok = true;
		for (int g = 2; g < n; g++) {
			if (abs(razn - abs(a[g] - a[g - 1])) >= 2) {
				ok = false;
				break;
			}
			else if(abs(razn - abs(a[g] - a[g - 1])) == 1)
				cnt++, a[g] -= (razn - abs(a[g] - a[g - 1]));
		}
		for (int i = 0; i < n; i++)
			a[i] = b[i];
		if(ok)
			ans = min(ans, cnt);
	}
	if (ans == 2e9)
		ans = -1;
	cout << ans;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define ll long long
#define endl "\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 5e5 + 5;

template <class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b, ll mod) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b, ll mod) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
struct comp
{
    bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const
    {
        return x.first < y.first;
    }
};
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll pwr(ll x, ll n, ll mod)
{
    if (n == 0)
        return 1;
    if (n & 1)
        return mul(x, pwr(mul(x, x, mod), (n - 1) / 2, mod), mod);
    else
        return pwr(mul(x, x, mod), n / 2, mod);
}
ll modInv(ll n, ll mod)
{
    return pwr(n, mod - 2, mod);
}
ll modInverse(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;

    if (m == 1)
        return 0;

    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }

    if (x < 0)
        x += m0;

    return x;
}
ll fact[N], invfact[N];

void precompute(ll mod)
{
    fact[0] = fact[1] = 1;
    for (int i = 2; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
    invfact[N - 1] = modInv(fact[N - 1], mod);
    for (int i = N - 2; i >= 0; i--)
    {
        invfact[i] = invfact[i + 1] * (i + 1);
        invfact[i] %= mod;
    }
}
int nCr(int x, int y, ll mod)
{
    if (y > x)
        return 0;
    ll num = fact[x];
    num *= invfact[y];
    num %= mod;
    num *= invfact[x - y];
    num %= mod;
    return num;
}
int a[N];
int n;
ll solve(int d, int s)
{
    int ans = 0;

    for (int i = 1; i < n; i++)
    {
        int next = s + d;
        //  cout << "next " << next << " a'[i] " << a[i] << endl;
        if (abs(next - a[i]) <= 1)
            ans += abs(next - a[i]);
        else
            return INT_MAX;
        s = s + d;
    }
    return ans;
}
int main()
{
    IOS;
    ll t = 1;

    while (t--)
    {

        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (n <= 2)
            cout << 0 << endl;
        else
        {
            ll ans = solve(a[1] - a[0], a[0]);
            ans = min(ans, 1 + solve(a[1] - a[0] + 1, a[0] - 1));
            ans = min(ans, 1 + solve(a[1] - a[0] - 1, a[0] + 1));
            ans = min(ans, 1 + solve(a[1] - a[0] + 2, a[0] - 1));
            ans = min(ans, 1 + solve(a[1] - a[0] - 2, a[0] + 1));
            if (ans != INT_MAX)
                cout << ans << endl;
            else
                cout << -1 << endl;
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define ll long long
#define endl "\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 5e5 + 5;

template <class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b, ll mod) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b, ll mod) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
struct comp
{
    bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const
    {
        return x.first < y.first;
    }
};
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll pwr(ll x, ll n, ll mod)
{
    if (n == 0)
        return 1;
    if (n & 1)
        return mul(x, pwr(mul(x, x, mod), (n - 1) / 2, mod), mod);
    else
        return pwr(mul(x, x, mod), n / 2, mod);
}
ll modInv(ll n, ll mod)
{
    return pwr(n, mod - 2, mod);
}
ll modInverse(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;

    if (m == 1)
        return 0;

    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }

    if (x < 0)
        x += m0;

    return x;
}
ll fact[N], invfact[N];

void precompute(ll mod)
{
    fact[0] = fact[1] = 1;
    for (int i = 2; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
    invfact[N - 1] = modInv(fact[N - 1], mod);
    for (int i = N - 2; i >= 0; i--)
    {
        invfact[i] = invfact[i + 1] * (i + 1);
        invfact[i] %= mod;
    }
}
int nCr(int x, int y, ll mod)
{
    if (y > x)
        return 0;
    ll num = fact[x];
    num *= invfact[y];
    num %= mod;
    num *= invfact[x - y];
    num %= mod;
    return num;
}
ll a[N];
int n;
ll solve(ll d, ll s)
{
    ll ans = 0;

    for (int i = 1; i < n; i++)
    {
        ll next = s + d;
        //  cout << "next " << next << " a'[i] " << a[i] << endl;
        if (abs(next - a[i]) <= 1)
            ans += abs(next - a[i]);
        else
            return INT_MAX;
        s = s + d;
    }
    return ans;
}
int main()
{
    IOS;
    ll t = 1;

    while (t--)
    {

        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (n <= 2)
            cout << 0 << endl;
        else
        {
            ll ans = solve(a[1] - a[0], a[0]);
            ans = min(ans, 1 + solve(a[1] - a[0] + 1, a[0] - 1));
            ans = min(ans, 1 + solve(a[1] - a[0] - 1, a[0] + 1));
            ans = min(ans, 1 + solve(a[1] - a[0] + 2, a[0] - 1));
            ans = min(ans, 1 + solve(a[1] - a[0] - 2, a[0] + 1));
            if (ans != INT_MAX)
                cout << ans << endl;
            else
                cout << -1 << endl;
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define ll long long
#define endl "\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 5e5 + 5;

template <class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b, ll mod) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b, ll mod) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
struct comp
{
    bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const
    {
        return x.first < y.first;
    }
};
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll pwr(ll x, ll n, ll mod)
{
    if (n == 0)
        return 1;
    if (n & 1)
        return mul(x, pwr(mul(x, x, mod), (n - 1) / 2, mod), mod);
    else
        return pwr(mul(x, x, mod), n / 2, mod);
}
ll modInv(ll n, ll mod)
{
    return pwr(n, mod - 2, mod);
}
ll modInverse(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;

    if (m == 1)
        return 0;

    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }

    if (x < 0)
        x += m0;

    return x;
}
ll fact[N], invfact[N];

void precompute(ll mod)
{
    fact[0] = fact[1] = 1;
    for (int i = 2; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
    invfact[N - 1] = modInv(fact[N - 1], mod);
    for (int i = N - 2; i >= 0; i--)
    {
        invfact[i] = invfact[i + 1] * (i + 1);
        invfact[i] %= mod;
    }
}
int nCr(int x, int y, ll mod)
{
    if (y > x)
        return 0;
    ll num = fact[x];
    num *= invfact[y];
    num %= mod;
    num *= invfact[x - y];
    num %= mod;
    return num;
}
ll a[N];
int n;
ll solve(ll d, ll s)
{
    ll ans = 0;
    ll x = s;
    for (int i = 0; i < n; i++)
    {

        if (abs(s - a[i]) <= 1)
            ans += abs(s - a[i]);
        else
            return INT_MAX;
        s = s + d;
    }
   // cout << "d " << d << " s " << x << " ans  " << ans << endl;
    return ans;
}
int main()
{
    IOS;
    ll t = 1;

    while (t--)
    {

        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (n <= 2)
            cout << 0 << endl;
        else
        {
            ll ans = solve(a[1] - a[0], a[0]);
            ans = min(ans, solve(a[1] - a[0] + 1, a[0] - 1));
            ans = min(ans, solve(a[1] - a[0] - 1, a[0] + 1));
            ans = min(ans, solve(a[1] - a[0] + 2, a[0] - 1));
            ans = min(ans, solve(a[1] - a[0] - 2, a[0] + 1));
            ans = min(ans, solve(a[1] - a[0] + 1, a[0]));
            ans = min(ans, solve(a[1] - a[0] - 1, a[0]));
            if (ans != INT_MAX)
                cout << ans << endl;
            else
                cout << -1 << endl;
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define ll long long
#define endl "\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 5e5 + 5;

template <class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b, ll mod) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b, ll mod) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
struct comp
{
    bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const
    {
        return x.first < y.first;
    }
};
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll pwr(ll x, ll n, ll mod)
{
    if (n == 0)
        return 1;
    if (n & 1)
        return mul(x, pwr(mul(x, x, mod), (n - 1) / 2, mod), mod);
    else
        return pwr(mul(x, x, mod), n / 2, mod);
}
ll modInv(ll n, ll mod)
{
    return pwr(n, mod - 2, mod);
}
ll modInverse(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;

    if (m == 1)
        return 0;

    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }

    if (x < 0)
        x += m0;

    return x;
}
ll fact[N], invfact[N];

void precompute(ll mod)
{
    fact[0] = fact[1] = 1;
    for (int i = 2; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
    invfact[N - 1] = modInv(fact[N - 1], mod);
    for (int i = N - 2; i >= 0; i--)
    {
        invfact[i] = invfact[i + 1] * (i + 1);
        invfact[i] %= mod;
    }
}
int nCr(int x, int y, ll mod)
{
    if (y > x)
        return 0;
    ll num = fact[x];
    num *= invfact[y];
    num %= mod;
    num *= invfact[x - y];
    num %= mod;
    return num;
}
ll a[N];
int n;
ll solve(ll d, ll s)
{
    ll ans = 0;
    ll x = s;
    for (int i = 0; i < n; i++)
    {

        if (abs(s - a[i]) <= 1)
            ans += abs(s - a[i]);
        else
            return INT_MAX;
        s = s + d;
    }
    // cout << "d " << d << " s " << x << " ans  " << ans << endl;
    return ans;
}
int main()
{
    IOS;
    ll t = 1;

    while (t--)
    {

        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (n <= 2)
            cout << 0 << endl;
        else
        {
            ll ans = solve(a[1] - a[0], a[0]);
            ans = min(ans, solve(a[1] - a[0] + 1, a[0]));
            ans = min(ans, solve(a[1] - a[0] - 1, a[0]));
            ans = min(ans, solve(a[1] - a[0], a[0] + 1));
            ans = min(ans, solve(a[1] - a[0] - 1, a[0] + 1));
            ans = min(ans, solve(a[1] - a[0] - 2, a[0] + 1));
            ans = min(ans, solve(a[1] - a[0], a[0] - 1));
            ans = min(ans, solve(a[1] - a[0] + 1, a[0] - 1));
            ans = min(ans, solve(a[1] - a[0] + 2, a[0] - 1));

            if (ans != INT_MAX)
                cout << ans << endl;
            else
                cout << -1 << endl;
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define ll long long
#define endl "\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 5e5 + 5;

template <class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b, ll mod) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b, ll mod) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
struct comp
{
    bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const
    {
        return x.first < y.first;
    }
};
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll pwr(ll x, ll n, ll mod)
{
    if (n == 0)
        return 1;
    if (n & 1)
        return mul(x, pwr(mul(x, x, mod), (n - 1) / 2, mod), mod);
    else
        return pwr(mul(x, x, mod), n / 2, mod);
}
ll modInv(ll n, ll mod)
{
    return pwr(n, mod - 2, mod);
}
ll modInverse(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;

    if (m == 1)
        return 0;

    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }

    if (x < 0)
        x += m0;

    return x;
}
ll fact[N], invfact[N];

void precompute(ll mod)
{
    fact[0] = fact[1] = 1;
    for (int i = 2; i < N; i++)
    {
        fact[i] = fact[i - 1] * i;
        fact[i] %= mod;
    }
    invfact[N - 1] = modInv(fact[N - 1], mod);
    for (int i = N - 2; i >= 0; i--)
    {
        invfact[i] = invfact[i + 1] * (i + 1);
        invfact[i] %= mod;
    }
}
int nCr(int x, int y, ll mod)
{
    if (y > x)
        return 0;
    ll num = fact[x];
    num *= invfact[y];
    num %= mod;
    num *= invfact[x - y];
    num %= mod;
    return num;
}
ll a[N];
int n;
ll solve(ll d, ll s)
{
    ll ans = 0;

    for (int i = 1; i < n; i++)
    {
        int next = s + d;
        //  cout << "next " << next << " a'[i] " << a[i] << endl;
        if (abs(next - a[i]) <= 1)
            ans += abs(next - a[i]);
        else
            return INT_MAX;
        s = s + d;
    }
    return ans;
}
int main()
{
    IOS;
    ll t = 1;

    while (t--)
    {

        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (n <= 2)
            cout << 0 << endl;
        else
        {
            ll ans = solve(a[1] - a[0], a[0]);
            ans = min(ans, 1 + solve(a[1] - a[0] + 1, a[0] - 1));
            ans = min(ans, 1 + solve(a[1] - a[0] - 1, a[0] + 1));
            ans = min(ans, 1 + solve(a[1] - a[0] + 2, a[0] - 1));
            ans = min(ans, 1 + solve(a[1] - a[0] - 2, a[0] + 1));
            if (ans != INT_MAX)
                cout << ans << endl;
            else
                cout << -1 << endl;
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,i,j,k;
ll a[100001];
ll get(ll x,ll y)
{
    //cout<<"x = "<<x<<" y = "<<y<<endl;
    ll dis=y-x;
    ll c=0;
    for(i=0;i<n;i++)
    {
        ll ok=(dis*i)+x;
        if(abs(ok-a[i])>1) return INT_MAX;
        else if(ok!=a[i]) c++;
    }
    return c;
}
int main()
{
    cin>>n;
    ll res=INT_MAX;
    for(i=0;i<n;i++) cin>>a[i];
    if(n<=2) return cout<<"0",0;
    for(j=-1;j<=1;j++)
    {
        for(k=-1;k<=1;k++)
        {
            ll found=get(a[0]+j,a[1]+k);
                res=min(res,found);
        }
    }
    if(res<INT_MAX) cout<<res<<endl;
    else cout<<"-1"<<endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,i,j,k;
ll a[100001];
ll get(ll x,ll y)
{
    //cout<<"x = "<<x<<" y = "<<y<<endl;
    ll dis=y-x;
    ll c=0;
    for(i=0;i<n;i++)
    {
        ll ok=(dis*i)+x;
        if(abs(ok-a[i])>1) return INT_MAX;
        else if(ok!=a[i]) c++;
    }
    return c;
}
int main()
{
    cin>>n;
    ll res=INT_MAX;
    for(i=0;i<n;i++) cin>>a[i];
    if(n<=2) return cout<<"0",0;
    for(j=-1;j<=1;j++)
    {
        for(k=-1;k<=1;k++)
        {
            ll found=get(a[0]+j,a[1]+k);
            if(found)
            {
                res=min(res,found);
            }
        }
    }
    if(res<INT_MAX) cout<<res<<endl;
    else cout<<"-1"<<endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,i,j,k;
ll a[100001];
ll get(ll x,ll y)
{
    cout<<"x = "<<x<<" y = "<<y<<endl;
    ll dis=y-x;
    ll c=0;
    for(i=0;i<n;i++)
    {
        ll ok=(dis*i)+x;
        if(abs(ok-a[i])>1) return 0;
        else if(ok!=a[i]) c++;
    }
    return c;
}
int main()
{
    cin>>n;
    ll res=INT_MAX;
    for(i=0;i<n;i++) cin>>a[i];
    if(n<=2) return cout<<"0",0;
    for(j=-1;j<=1;j++)
    {
        for(k=-1;k<=1;k++)
        {
            ll found=get(a[0]+j,a[1]+k);
            if(found)
            {
                res=min(res,found);
            }
        }
    }
    if(res<INT_MAX) cout<<res<<endl;
    else cout<<"-1"<<endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,i,j,k;
ll a[100001];
ll get(ll x,ll y)
{
    //cout<<"x = "<<x<<" y = "<<y<<endl;
    ll dis=y-x;
    ll c=0;
    for(i=0;i<n;i++)
    {
        ll ok=(dis*i)+x;
        if(abs(ok-a[i])>1) return 0;
        else if(ok!=a[i]) c++;
    }
    return c;
}
int main()
{
    cin>>n;
    ll res=INT_MAX;
    for(i=0;i<n;i++) cin>>a[i];
    if(n<=2) return cout<<"0",0;
    for(j=-1;j<=1;j++)
    {
        for(k=-1;k<=1;k++)
        {
            ll found=get(a[0]+j,a[1]+k);
            if(found)
            {
                res=min(res,found);
            }
        }
    }
    if(res<INT_MAX) cout<<res<<endl;
    else cout<<"-1"<<endl;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], dp[maxn], ans;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	if (n <= 2){
		cout << 0;
		return 0;
	}

	for (int i = 1; i < n; i++){
		dp[i] = a[i + 1] - a[i];
	}

	if (n == 3 && abs(dp[2] - dp[1]) <= 2){
		cout << 1;
		return 0;
	}
	else{
		cout << -1;
		return 0;
	}

	for (int i = 2; i < n - 1; i++){
		if (dp[i] > dp[i - 1] && abs(dp[i] - dp[i - 1]) > 1)
			dp[i]--, dp[i - 1]++, ans++;
		else if (dp[i] < dp[i - 1] && abs(dp[i] - dp[i - 1]) > 1)
			dp[i]++, dp[i - 1]--, ans++;
		if (dp[i] > dp[i + 1] && abs(dp[i + 1] - dp[i]) > 1)
			dp[i]--, dp[i + 1]++, ans++;
		else if (dp[i] < dp[i + 1] && abs(dp[i + 1] - dp[i]) > 1)
			dp[i]++, dp[i + 1]--, ans++;
	}

	if (dp[1] < dp[2] && abs(dp[2] - dp[1]) == 1)
		dp[1]++, ans++;
	else if (dp[1] > dp[2] && abs(dp[2] - dp[1]) == 1)
		dp[1]--, ans++;

	if (dp[n - 1] < dp[n - 2] && abs(dp[n - 2] - dp[n - 1]) == 1)
		dp[n - 1]++, ans++;
	else if (dp[n - 1] > dp[n - 2] && abs(dp[n - 2] - dp[n - 1]) == 1)
		dp[n - 1]--, ans++;

	set <int> s;

	for (int i = 1; i < n; i++)
		s.insert(dp[i]);

	if (sz(s) == 1)
		cout << ans;
	else
		cout << -1;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], dp[maxn], ans;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	if (n <= 2){
		cout << 0;
		return 0;
	}

	for (int i = 1; i < n; i++){
		dp[i] = a[i + 1] - a[i];
	}

	if (n == 3 && abs(dp[2] - dp[1]) <= 2 && abs(dp[2] - dp[1]) > 0){
		cout << 1;
		return 0;
	}
	else if (n == 3 && abs(dp[2] - dp[1]) == 0){
		cout << 0;
		return 0;
	}
	else if (n == 3){
		cout << -1;
		return 0;
	}

	for (int i = 2; i < n - 1; i++){
		if (dp[i] > dp[i - 1] && abs(dp[i] - dp[i - 1]) > 1)
			dp[i]--, dp[i - 1]++, ans++;
		else if (dp[i] < dp[i - 1] && abs(dp[i] - dp[i - 1]) > 1)
			dp[i]++, dp[i - 1]--, ans++;
		if (dp[i] > dp[i + 1] && abs(dp[i + 1] - dp[i]) > 1)
			dp[i]--, dp[i + 1]++, ans++;
		else if (dp[i] < dp[i + 1] && abs(dp[i + 1] - dp[i]) > 1)
			dp[i]++, dp[i + 1]--, ans++;
	}

	if (dp[1] < dp[2] && abs(dp[2] - dp[1]) == 1)
		dp[1]++, ans++;
	else if (dp[1] > dp[2] && abs(dp[2] - dp[1]) == 1)
		dp[1]--, ans++;

	if (dp[n - 1] < dp[n - 2] && abs(dp[n - 2] - dp[n - 1]) == 1)
		dp[n - 1]++, ans++;
	else if (dp[n - 1] > dp[n - 2] && abs(dp[n - 2] - dp[n - 1]) == 1)
		dp[n - 1]--, ans++;
	set <int> s;

	for (int i = 1; i < n; i++)
		s.insert(dp[i]);

	if (sz(s) == 1)
		cout << ans;
	else
		cout << -1;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], dp[maxn], ans;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	for (int i = 1; i < n; i++){
		dp[i] = a[i + 1] - a[i];
	}

	for (int i = 1; i < n; i++){
		if (i == 1){
			if (dp[i] < dp[i + 1])
				dp[i]++, ans++;
			else if (dp[i] > dp[i + 1])
				dp[i]--, ans++;
		}
		else{
			if (dp[i] > dp[i - 1])
				dp[i]--, dp[i - 1]++, ans++;
			else if (dp[i] < dp[i - 1])
				dp[i]++, dp[i - 1]--, ans++;
		}
	}

	set <int> s;

	for (int i = 1; i < n; i++)
		s.insert(dp[i]);

	if (sz(s) == 1)
		cout << ans;
	else
		cout << -1;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], dp[maxn], ans;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	if (n <= 2){
		cout << 0;
		return 0;
	}

	for (int i = 1; i < n; i++){
		dp[i] = a[i + 1] - a[i];
	}

	for (int i = 1; i < n; i++)
		cout << dp[i] << " ";
	cout << endl;

	for (int i = 1; i < n; i++){
		if (i == 1){
			if (dp[i] < dp[i + 1])
				dp[i]++, ans++;
			else if (dp[i] > dp[i + 1])
				dp[i]--, ans++;
		}
		else{
			if (dp[i] > dp[i - 1])
				dp[i]--, dp[i - 1]++, ans++;
			else if (dp[i] < dp[i - 1])
				dp[i]++, dp[i - 1]--, ans++;
			if (dp[i] > dp[i + 1] && i + 1 < n)
				dp[i]--, dp[i + 1]++, ans++;
			else if (dp[i] < dp[i + 1] && i + 1 < n)
				dp[i]++, dp[i + 1]--, ans++;
		}
	}

	set <int> s;

	for (int i = 1; i < n; i++)
		s.insert(dp[i]);

	if (sz(s) == 1)
		cout << ans;
	else
		cout << -1;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], b[maxn], c[maxn], ans;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	if (n <= 2){
		cout << 0;
		return 0;
	}

	for (int i = -1; i <= 1; i++){
		for (int j = -1; j <= 1; j++){
			int x = a[1], y = a[2];
			x += i;
			y += j;
			int k = y - x;
			b[1] = x;
			c[1] = x;
			for (int d = 2; d <= n; d++){
				b[d] = b[d - 1] + k;
			}
			for (int d = 2; d <= n; d++){
				c[d] = c[d - 1] - k;
			}
			ans = 0;
			bool ch = true;
			for (int d = 1; d <= n; d++){
				if (abs(b[d] - a[d]) == 1)
					ans++;
				else if (abs(b[d] - a[d]) > 1){
					ch = false;
					break;
				}
			}
			if (ch){
				cout << ans;
				return 0;
			}
			ans = 0;
			ch = true;
			for (int d = 1; d <= n; d++){
				if (abs(c[d] - a[d]) == 1)
					ans++;
				else if (abs(c[d] - a[d]) > 1){
					ch = false;
					break;
				}
			}
			if (ch){
				cout << ans;
				return 0;
			}
		}
	}

	cout << -1;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], dp[maxn], ans;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	if (n <= 2){
		cout << 0;
		return 0;
	}

	for (int i = 1; i < n; i++){
		dp[i] = a[i + 1] - a[i];
	}

	for (int i = 1; i < n; i++){
		if (i == 1){
			if (dp[i] < dp[i + 1])
				dp[i]++, ans++;
			else if (dp[i] > dp[i + 1])
				dp[i]--, ans++;
		}
		else{
			if (dp[i] > dp[i - 1])
				dp[i]--, dp[i - 1]++, ans++;
			else if (dp[i] < dp[i - 1])
				dp[i]++, dp[i - 1]--, ans++;
			if (dp[i] > dp[i + 1] && i + 1 < n)
				dp[i]--, dp[i + 1]++, ans++;
			else if (dp[i] < dp[i + 1] && i + 1 < n)
				dp[i]++, dp[i + 1]--, ans++;
		}
	}

	set <int> s;

	for (int i = 1; i < n; i++)
		s.insert(dp[i]);

	if (sz(s) == 1)
		cout << ans;
	else
		cout << -1;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], dp[maxn], ans;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	if (n <= 2){
		cout << 0;
		return 0;
	}

	for (int i = 1; i < n; i++){
		dp[i] = a[i + 1] - a[i];
	}

	if (n == 3 && abs(dp[2] - dp[1]) <= 2){
		cout << 1;
		return 0;
	}
	else if (n == 3){
		cout << -1;
		return 0;
	}

	for (int i = 2; i < n - 1; i++){
		if (dp[i] > dp[i - 1] && abs(dp[i] - dp[i - 1]) > 1)
			dp[i]--, dp[i - 1]++, ans++;
		else if (dp[i] < dp[i - 1] && abs(dp[i] - dp[i - 1]) > 1)
			dp[i]++, dp[i - 1]--, ans++;
		if (dp[i] > dp[i + 1] && abs(dp[i + 1] - dp[i]) > 1)
			dp[i]--, dp[i + 1]++, ans++;
		else if (dp[i] < dp[i + 1] && abs(dp[i + 1] - dp[i]) > 1)
			dp[i]++, dp[i + 1]--, ans++;
	}

	if (dp[1] < dp[2] && abs(dp[2] - dp[1]) == 1)
		dp[1]++, ans++;
	else if (dp[1] > dp[2] && abs(dp[2] - dp[1]) == 1)
		dp[1]--, ans++;

	if (dp[n - 1] < dp[n - 2] && abs(dp[n - 2] - dp[n - 1]) == 1)
		dp[n - 1]++, ans++;
	else if (dp[n - 1] > dp[n - 2] && abs(dp[n - 2] - dp[n - 1]) == 1)
		dp[n - 1]--, ans++;
	set <int> s;

	for (int i = 1; i < n; i++)
		s.insert(dp[i]);

	if (sz(s) == 1)
		cout << ans;
	else
		cout << -1;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], b[maxn], c[maxn], ans, z = inf;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	if (n <= 2){
		cout << 0;
		return 0;
	}

	for (int i = -1; i <= 1; i++){
		for (int j = -1; j <= 1; j++){
			int x = a[1], y = a[2];
			x += i;
			y += j;
			int k = y - x;
			b[1] = x;
			c[1] = x;
			for (int d = 2; d <= n; d++){
				b[d] = b[d - 1] + k;
			}
			for (int d = 2; d <= n; d++){
				c[d] = c[d - 1] - k;
			}
			ans = 0;
			bool ch = true;
			for (int d = 1; d <= n; d++){
				if (abs(b[d] - a[d]) == 1)
					ans++;
				else if (abs(b[d] - a[d]) > 1){
					ch = false;
					break;
				}
			}
			if (ch){
				z = min(ans, z);
			}
			ans = 0;
			ch = true;
			for (int d = 1; d <= n; d++){
				if (abs(c[d] - a[d]) == 1)
					ans++;
				else if (abs(c[d] - a[d]) > 1){
					ch = false;
					break;
				}
			}
			if (ch){
				z = min(ans, z);
			}
		}
	}

	if (z == inf)
		cout << -1;
	else
		cout << z;
}


//by Esma
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <cmath>
#include <cstdio>
#include <iomanip>

#define f first
#define s second
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, n) for (int i = 1; i <= n; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return

const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 111;

using namespace std;

int n, a[maxn], dp[maxn], ans;

int main(){
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> a[i];

	if (n <= 2){
		cout << 0;
		return 0;
	}

	for (int i = 1; i < n; i++){
		dp[i] = a[i + 1] - a[i];
	}

	for (int i = 1; i < n; i++){
		if (i == 1){
			if (dp[i] < dp[i + 1])
				dp[i]++, ans++;
			else if (dp[i] > dp[i + 1])
				dp[i]--, ans++;
		}
		else{
			if (dp[i] > dp[i - 1])
				dp[i]--, dp[i - 1]++, ans++;
			else if (dp[i] < dp[i - 1])
				dp[i]++, dp[i - 1]--, ans++;
		}
	}

	set <int> s;

	for (int i = 1; i < n; i++)
		s.insert(dp[i]);

	if (sz(s) == 1)
		cout << ans;
	else
		cout << -1;
}


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define PI 2*acos(0)
#define INF 1e17
#define mx 200005
using namespace std;
//const ll int p=1000000007;
//vector<ll > prime;
//bool mark[mx];
ll int mod(ll int a,ll int b)
{
    ll int m=a%b;
    if(m<0) m=(b<0)?(m-b):(m+b);
    return m;
}
ll maxpow(ll n)
{
    return (n & (~(n-1)));
}
bool isPrime(ll n)
{
    if(n<2) return false;
    if(n==2) return true;
    if(n%2==0) return false;
    for(ll i=3;i*i<=n;i+=2){
        if(n%i==0) return false;
    }
    return true;
}
/*void init()
{
    mark[1]=1;
    int i,j,limit=sqrt(mx*1.0)+2;
    prime.push_back(2);
    for(i=4;i<mx;i+=2) mark[i]=1;
    mark[2]=0;
    for(i=3;i<mx;i+=2){
        if(!mark[i]){
            prime.push_back(i);
            if(i<=limit){
                for(j=i*i;j<mx;j+=2*i){
                    mark[j]=1;
                }
            }
        }
    }
}
/*
ll maxdiv(ll n)
{
    ll i,ans=1;
    for(i=0;i<prime.size();i++){
        if(n%prime[i]==0){
            ans=n/prime[i];break;
            //return ans;
        }
    }
    return ans;
}*/
ll lcm(ll a,ll b)
{
    ll ans=a*b;
    return (ans/__gcd(a,b));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll cnt,n,i,j,k,l,u,v,x,z,w,sum,ans;
    cin>>n;
    ll b[n],c[n];
    for(i=0;i<n;i++) cin>>b[i];
    if(n<=2){
        cout<<0<<endl;
        return 0;
    }
    u=b[0];v=b[1];//cout<<u<<" "<<v<<endl;
    ans=INF;bool tr=1;
    for(i=u-1;i<=u+1;i++){
        for(j=v-1;j<=v+1;j++){
            x=j-i;tr=1;c[0]=i;c[1]=j;
            //cout<<"x="<<x<<endl;
            for(k=2;k<n;k++){
                c[k]=c[k-1]+x;
                //cout<<z<<endl;
                if(c[k] > b[k]+1 || c[k] < b[k]-1){
                    tr=0;break;
                }
            }
            if(!tr) continue;
            cnt=0;
            if(c[0]!=b[0]) cnt++;
            if(c[1]!=b[1]) cnt++;
            for(k=2;k<n;k++){
                if(c[k]!=b[k]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    if(ans==INF){
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}
//1028C


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define PI 2*acos(0)
#define INF 1e17
#define mx 200005
using namespace std;
//const ll int p=1000000007;
//vector<ll > prime;
//bool mark[mx];
ll int mod(ll int a,ll int b)
{
    ll int m=a%b;
    if(m<0) m=(b<0)?(m-b):(m+b);
    return m;
}
ll maxpow(ll n)
{
    return (n & (~(n-1)));
}
bool isPrime(ll n)
{
    if(n<2) return false;
    if(n==2) return true;
    if(n%2==0) return false;
    for(ll i=3;i*i<=n;i+=2){
        if(n%i==0) return false;
    }
    return true;
}
/*void init()
{
    mark[1]=1;
    int i,j,limit=sqrt(mx*1.0)+2;
    prime.push_back(2);
    for(i=4;i<mx;i+=2) mark[i]=1;
    mark[2]=0;
    for(i=3;i<mx;i+=2){
        if(!mark[i]){
            prime.push_back(i);
            if(i<=limit){
                for(j=i*i;j<mx;j+=2*i){
                    mark[j]=1;
                }
            }
        }
    }
}
/*
ll maxdiv(ll n)
{
    ll i,ans=1;
    for(i=0;i<prime.size();i++){
        if(n%prime[i]==0){
            ans=n/prime[i];break;
            //return ans;
        }
    }
    return ans;
}*/
ll lcm(ll a,ll b)
{
    ll ans=a*b;
    return (ans/__gcd(a,b));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll cnt,n,i,j,k,l,u,v,x,z,w,sum,ans;
    cin>>n;
    ll b[n];
    for(i=0;i<n;i++) cin>>b[i];
    if(n<=2){
        cout<<0<<endl;
        return 0;
    }
    ll d=b[1]-b[0];
    bool tr=1;
    for(i=1;i+1<n;i++){
        if(b[i+1]-b[i]!=d){
            tr=0;
            break;
        }
    }
    if(tr){
        cout<<0<<endl;
        return 0;
    }
    ll diff[n-1];
    for(i=0;i+1<n;i++){
        diff[i]=b[i+1]-b[i];
    }
    x=-1;
    z=INF;
    for(i=0;i<n-1;i++){
        z=min(diff[i],z);
        x=max(x,diff[i]);
    }
    if(x-z>4) tr=0;
    if(!tr){
        cout<<-1<<endl;
        return 0;
    }
    bool t[5]={0,0,0,0,0};
    ll a[5][n];
    a[0][0]=b[0];
    a[1][0]=b[0];
    a[2][0]=b[0];
    a[3][0]=b[0];
    a[4][0]=b[0];
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    a[0][0]=b[0]+1;
    a[1][0]=b[0]+1;
    a[2][0]=b[0]+1;
    a[3][0]=b[0]+1;
    a[4][0]=b[0]+1;
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    //ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    a[0][0]=b[0]-1;
    a[1][0]=b[0]-1;
    a[2][0]=b[0]-1;
    a[3][0]=b[0]-1;
    a[4][0]=b[0]-1;
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    //ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    cout<<ans<<endl;
    return 0;
}
//1028C


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define PI 2*acos(0)
#define INF 1e17
#define mx 200005
using namespace std;
//const ll int p=1000000007;
//vector<ll > prime;
//bool mark[mx];
ll int mod(ll int a,ll int b)
{
    ll int m=a%b;
    if(m<0) m=(b<0)?(m-b):(m+b);
    return m;
}
ll maxpow(ll n)
{
    return (n & (~(n-1)));
}
bool isPrime(ll n)
{
    if(n<2) return false;
    if(n==2) return true;
    if(n%2==0) return false;
    for(ll i=3;i*i<=n;i+=2){
        if(n%i==0) return false;
    }
    return true;
}
/*void init()
{
    mark[1]=1;
    int i,j,limit=sqrt(mx*1.0)+2;
    prime.push_back(2);
    for(i=4;i<mx;i+=2) mark[i]=1;
    mark[2]=0;
    for(i=3;i<mx;i+=2){
        if(!mark[i]){
            prime.push_back(i);
            if(i<=limit){
                for(j=i*i;j<mx;j+=2*i){
                    mark[j]=1;
                }
            }
        }
    }
}
/*
ll maxdiv(ll n)
{
    ll i,ans=1;
    for(i=0;i<prime.size();i++){
        if(n%prime[i]==0){
            ans=n/prime[i];break;
            //return ans;
        }
    }
    return ans;
}*/
ll lcm(ll a,ll b)
{
    ll ans=a*b;
    return (ans/__gcd(a,b));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll cnt,n,i,j,k,l,u,v,x,z,w,sum,ans;
    cin>>n;
    ll b[n];
    for(i=0;i<n;i++) cin>>b[i];
    if(n<=2){
        cout<<0<<endl;
        return 0;
    }
    ll d=b[1]-b[0];
    bool tr=1;
    for(i=1;i+1<n;i++){
        if(b[i+1]-b[i]!=d){
            tr=0;
            break;
        }
    }
    if(tr){
        cout<<0<<endl;
        return 0;
    }
    ll diff[n-1];
    for(i=0;i+1<n;i++){
        diff[i]=b[i+1]-b[i];
    }
    x=-INF;
    z=INF;
    for(i=0;i<n-1;i++){
        z=min(diff[i],z);
        x=max(x,diff[i]);
    }
    tr=1;
    if(x-z > 4) tr=0;
   //cout<<x<<" "<<z<<endl;
    if(!tr){
        cout<<-1<<endl;
        return 0;
    }
    bool t[5]={0,0,0,0,0};
    ll a[5][n];
    a[0][0]=b[0];
    a[1][0]=b[0];
    a[2][0]=b[0];
    a[3][0]=b[0];
    a[4][0]=b[0];
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    a[0][0]=b[0]+1;
    a[1][0]=b[0]+1;
    a[2][0]=b[0]+1;
    a[3][0]=b[0]+1;
    a[4][0]=b[0]+1;
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    //ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    a[0][0]=b[0]-1;
    a[1][0]=b[0]-1;
    a[2][0]=b[0]-1;
    a[3][0]=b[0]-1;
    a[4][0]=b[0]-1;
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    //ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    cout<<ans<<endl;
    return 0;
}
//1028C


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define PI 2*acos(0)
#define INF 1e17
#define mx 200005
using namespace std;
//const ll int p=1000000007;
//vector<ll > prime;
//bool mark[mx];
ll int mod(ll int a,ll int b)
{
    ll int m=a%b;
    if(m<0) m=(b<0)?(m-b):(m+b);
    return m;
}
ll maxpow(ll n)
{
    return (n & (~(n-1)));
}
bool isPrime(ll n)
{
    if(n<2) return false;
    if(n==2) return true;
    if(n%2==0) return false;
    for(ll i=3;i*i<=n;i+=2){
        if(n%i==0) return false;
    }
    return true;
}
/*void init()
{
    mark[1]=1;
    int i,j,limit=sqrt(mx*1.0)+2;
    prime.push_back(2);
    for(i=4;i<mx;i+=2) mark[i]=1;
    mark[2]=0;
    for(i=3;i<mx;i+=2){
        if(!mark[i]){
            prime.push_back(i);
            if(i<=limit){
                for(j=i*i;j<mx;j+=2*i){
                    mark[j]=1;
                }
            }
        }
    }
}
/*
ll maxdiv(ll n)
{
    ll i,ans=1;
    for(i=0;i<prime.size();i++){
        if(n%prime[i]==0){
            ans=n/prime[i];break;
            //return ans;
        }
    }
    return ans;
}*/
ll lcm(ll a,ll b)
{
    ll ans=a*b;
    return (ans/__gcd(a,b));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll cnt,n,i,j,k,l,u,v,x,z,w,sum,ans;
    cin>>n;
    ll b[n];
    for(i=0;i<n;i++) cin>>b[i];
    if(n<=2){
        cout<<0<<endl;
        return 0;
    }
    ll d=b[1]-b[0];
    bool tr=1;
    for(i=1;i+1<n;i++){
        if(b[i+1]-b[i]!=d){
            tr=0;
            break;
        }
    }
    if(tr){
        cout<<0<<endl;
        return 0;
    }
    ll diff[n-1];
    for(i=0;i+1<n;i++){
        diff[i]=b[i+1]-b[i];
    }
    x=-1;
    z=INF;
    for(i=0;i<n-1;i++){
        z=min(diff[i],z);
        x=max(x,diff[i]);
    }
    if(x-z>4) tr=1;
    if(!tr){
        cout<<-1<<endl;
        return 0;
    }
    bool t[5]={0,0,0,0,0};
    ll a[5][n];
    a[0][0]=b[0];
    a[1][0]=b[0];
    a[2][0]=b[0];
    a[3][0]=b[0];
    a[4][0]=b[0];
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    a[0][0]=b[0]+1;
    a[1][0]=b[0]+1;
    a[2][0]=b[0]+1;
    a[3][0]=b[0]+1;
    a[4][0]=b[0]+1;
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    //ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    a[0][0]=b[0]-1;
    a[1][0]=b[0]-1;
    a[2][0]=b[0]-1;
    a[3][0]=b[0]-1;
    a[4][0]=b[0]-1;
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    //ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    cout<<ans<<endl;
    return 0;
}
//1028C


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define PI 2*acos(0)
#define INF 1e17
#define mx 200005
using namespace std;
//const ll int p=1000000007;
//vector<ll > prime;
//bool mark[mx];
ll int mod(ll int a,ll int b)
{
    ll int m=a%b;
    if(m<0) m=(b<0)?(m-b):(m+b);
    return m;
}
ll maxpow(ll n)
{
    return (n & (~(n-1)));
}
bool isPrime(ll n)
{
    if(n<2) return false;
    if(n==2) return true;
    if(n%2==0) return false;
    for(ll i=3;i*i<=n;i+=2){
        if(n%i==0) return false;
    }
    return true;
}
/*void init()
{
    mark[1]=1;
    int i,j,limit=sqrt(mx*1.0)+2;
    prime.push_back(2);
    for(i=4;i<mx;i+=2) mark[i]=1;
    mark[2]=0;
    for(i=3;i<mx;i+=2){
        if(!mark[i]){
            prime.push_back(i);
            if(i<=limit){
                for(j=i*i;j<mx;j+=2*i){
                    mark[j]=1;
                }
            }
        }
    }
}
/*
ll maxdiv(ll n)
{
    ll i,ans=1;
    for(i=0;i<prime.size();i++){
        if(n%prime[i]==0){
            ans=n/prime[i];break;
            //return ans;
        }
    }
    return ans;
}*/
ll lcm(ll a,ll b)
{
    ll ans=a*b;
    return (ans/__gcd(a,b));
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll cnt,n,i,j,k,l,u,v,x,z,w,sum,ans;
    cin>>n;
    ll b[n];
    for(i=0;i<n;i++) cin>>b[i];
    if(n<=2){
        cout<<0<<endl;
        return 0;
    }
    ll d=b[1]-b[0];
    bool tr=1;
    for(i=1;i+1<n;i++){
        if(b[i+1]-b[i]!=d){
            tr=0;
            break;
        }
    }
    if(tr){
        cout<<0<<endl;
        return 0;
    }
    ll diff[n-1];
    for(i=0;i+1<n;i++){
        diff[i]=b[i+1]-b[i];
    }
    z=INF;
    for(i=0;i<n-1;i++) z=min(diff[i],z);
    tr=1;
    for(i=0;i<n-1;i++){
        if(diff[i]>z+4){
            tr=0;
            break;
        }
    }
    if(!tr){
        cout<<-1<<endl;
        return 0;
    }
    bool t[5]={0,0,0,0,0};
    ll a[5][n];
    a[0][0]=b[0];
    a[1][0]=b[0];
    a[2][0]=b[0];
    a[3][0]=b[0];
    a[4][0]=b[0];
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    a[0][0]=b[0]+1;
    a[1][0]=b[0]+1;
    a[2][0]=b[0]+1;
    a[3][0]=b[0]+1;
    a[4][0]=b[0]+1;
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    //ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    a[0][0]=b[0]-1;
    a[1][0]=b[0]-1;
    a[2][0]=b[0]-1;
    a[3][0]=b[0]-1;
    a[4][0]=b[0]-1;
    for(i=0;i<5;i++){
        x=z+i;
        t[i]=1;
        for(j=1;j<n;j++){
            a[i][j]=a[i][j-1]+x;
            if(a[i][j]>b[j]+1 || a[i][j]<b[j]-1){
                t[i]=0;
                break;
            }
        }
    }
    //for(i=0;i<5;i++) cout<<t[i]<<endl;
    //ans=INF;
    for(i=0;i<5;i++){
        if(t[i]){
            cnt=0;
            for(j=0;j<n;j++){
                if(a[i][j]!=b[j]) cnt++;
            }
            ans=min(ans,cnt);
        }
    }
    cout<<ans<<endl;
    return 0;
}
//1028C


#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    int n = 0; cin >> n;
    int seq[n];
    for(int i = 0; i < n; i++){
        seq[i] = 0; cin >> seq[i];
    }
    //idea is to use the possible differences based on first pair
    //then for each run through the sequence
    vector <int> vi;
    for(int i = -1; i <= 1; i++){
        for(int j = -1; j <= 1; j++){
            int count = 0;
            int d = seq[1] - seq[0] + i + j;
            if(abs(d - (seq[1] - seq[0])) == 2) count += 2;
            if(abs(d - (seq[1] - seq[0])) == 1) count += 1;
            int c = 2;
            for(int k = 2; k < n; k++){
                int del = abs(seq[k] - (seq[0] - j + k*d));
                if(del < 2){
                    if(del != 0){
                        count ++;
                    }
                }
                else{
                    count  = -1;
                    break;
                }
            }
            if(count != -1) vi.push_back(count);
        }
    }
    if(vi.size() == 0) vi.push_back(-1);
    sort(vi.begin(), vi.end()); cout << vi[0] << endl;
}


#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

long long mn = 1e9;
using namespace std;

int main(){
    int l = 0;
    int n = 0; cin >> n;
    int seq[n];
    for(int i = 0; i < n; i++){
        seq[i] = 0; cin >> seq[i];
    }
    //idea is to use the possible differences based on first pair
    //then for each run through the sequence
    vector <int> vi;
    for(int i = -1; i <= 1; i++){
        for(int j = -1; j <= 1; j++){
            int count = 0;
            int d = seq[1] - seq[0] + i + j;
            count += abs(j); int k;
            for(k = 1; k < n; k++){
                int del = abs(seq[k] - (seq[0] - j + k*d));
                if(del < 2) count += abs(del);
                else{
                    count  = -1;
                    break;
                }
            }
            //answer extraction
            if(k==n){
                if(mn > count) mn = count;
            }
        }
    }
    cout << (mn < 1e9 ? mn:-1) << endl;
}


#include <fstream>
#include <iostream>
#include <cmath>

using namespace std;

int main(){
//initializations
    /*ifstream fin ("test.in");
    ofstream fout ("test.out");*/
    int N = 0; cin >> N; //fin >> N;
    int seq[N]; int delta[N - 1]; int delta_copy[N - 1]; float avg_delta = 0.0;
    for(int i = 0; i < N; i++){
        cin >> seq[i]; //fin >> seq[i];
        if(i >= 1){
            delta[i - 1] = seq[i] - seq[i - 1];
            delta_copy[i - 1] = delta[i - 1];
            avg_delta += delta[i - 1];
        }
    }
    avg_delta /= N - 1;
    int u = ceil(avg_delta); int d = u - 1;

//testing for diff = u
    int count1 = 0;
    bool fail1 = false;
    //fout << "u = " << u << endl;
    if(delta[0] < u){
        delta[0] ++;
        count1 ++;
    }
    else if(delta[0] > u){
        delta[0] --;
        count1++;
    }
    //fout << "before loop " << count1 << endl;
    for(int i = 1; i < N; i++){
        //fout << "checking: " << delta[i - 1] << endl;
        if(abs(delta[i - 1] - u) >= 2){
            //fout << "failed" << endl;
            fail1 = true;
            count1 = -1;
            //fout << count1 << endl;
            break;
        }
        else if(delta[i - 1] > u){
            delta[i - 1] --;
            delta[i] ++;
            count1++;
        }
        else if(delta[i - 1] < u){
            delta[i - 1] ++;
            delta[i] --;
            count1++;
        }
        else count1+= 0;
        //fout << count1 << endl;
        //fout << endl;
    }
    if(fail1 == false){
        if(delta[N - 2] < u){
            delta[N - 2] ++;
            count1 ++;
        }
        else if(delta[N - 2] > u){
            delta[N - 2] --;
            count1++;
        }
    }
    //fout << count1 << endl;

    //fout << endl;
//testing for diff = d
    int count2 = 0;
    //fout << "d = " << d << endl;
    if(delta_copy[0] < d){
        delta_copy[0] ++;
        count2 ++;
    }
    else if(delta_copy[0] > d){
        delta_copy[0] --;
        count2++;
    }
    else{
        count2 += 0;
    }
    //fout << "before loop " << count2 << endl;
    bool fail2 = false;
    for(int i = 1; i < N; i++){
        //fout << "checking: " << delta_copy[i - 1] << endl;
        if(abs(delta_copy[i - 1] - d) >= 2){
            //fout << "failed " << endl;
            fail2 = true;
            count2 = -1;
            //fout << count2 << endl;
            break;
        }
        else if(delta_copy[i - 1] > d){
            delta_copy[i - 1] --;
            delta_copy[i] ++;
            count2++;
        }
        else if(delta_copy[i - 1] < d){
            delta_copy[i - 1] ++;
            delta_copy[i] --;
            count2++;
        }
        else count2 += 0;
        //fout << count2 << endl;
        //fout << endl;
    }
    if(fail2 == false){
        if(delta_copy[N - 2] < d){
            delta_copy[N - 2] ++;
            count2 ++;
        }
        else if(delta_copy[N - 2] > d){
            delta_copy[N - 2] --;
            count2++;
        }
    //fout << count2 << endl;
    }
//fout << "answer: " << endl;
//answer extraction
    if(count1 < count2){
        if(count1 != -1) cout << count1 << endl;
        else cout << count2 << endl;
    }
    if(count1 >= count2){
        if(count2 != -1) cout << count2 << endl;
        else cout << count1 << endl;
    }
}


#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

int main(){
    //ifstream fin ("test.in");
    //ofstream fout ("test.out");
    int N = 0; cin >> N;
    int seq[N]; int delta[N - 1]; int delta_copy[N - 1]; int delta_c[N - 1]; float avg_delta = 0.0;
    for(int i = 0; i < N; i++){
        cin >> seq[i];
        if(i >= 1){
            delta[i - 1] = seq[i] - seq[i - 1];
            delta_copy[i - 1] = delta[i - 1];
            delta_c[i - 1] = delta[i - 1];
            avg_delta += delta[i - 1];
        }
    }
    avg_delta /= N - 1;
    /*for(int i = 0; i < N; i++){
        fout << seq[i] << " ";
    }fout << endl;*/
    /*for(int i = 0; i < N - 1; i++){
        fout << delta[i] << " ";
    }fout << endl;*/
    int u = ceil(avg_delta); int d = u - 1; int e = u + 1;

//testing for diff = u
    int count1 = 0;
    bool fail1 = false;
    //fout << "u = " << u << endl;
    if(delta[0] < u){
        delta[0] ++;
        count1 ++;
    }
    else if(delta[0] > u){
        delta[0] --;
        count1++;
    }
    //fout << "before loop " << count1 << endl;
    for(int i = 1; i < N; i++){
        //fout << "checking: " << delta[i - 1] << endl;
        if(abs(delta[i - 1] - u) >= 2){
            //fout << "failed" << endl;
            fail1 = true;
            count1 = -1;
            //fout << count1 << endl;
            break;
        }
        else if(delta[i - 1] > u){
            delta[i - 1] --;
            delta[i] ++;
            count1++;
        }
        else if(delta[i - 1] < u){
            delta[i - 1] ++;
            delta[i] --;
            count1++;
        }
        else count1+= 0;
        //fout << count1 << endl;
        //fout << endl;
    }
    if(fail1 == false){
        if(delta[N - 2] < u){
            delta[N - 2] ++;
            count1 ++;
        }
        else if(delta[N - 2] > u){
            delta[N - 2] --;
            count1++;
        }
    }
    //fout << count1 << endl;

    //fout << endl;
//testing for diff = d
    int count2 = 0;
    //fout << "d = " << d << endl;
    if(delta_copy[0] < d){
        delta_copy[0] ++;
        count2 ++;
    }
    else if(delta_copy[0] > d){
        delta_copy[0] --;
        count2++;
    }
    else{
        count2 += 0;
    }
    //fout << "before loop " << count2 << endl;
    bool fail2 = false;
    for(int i = 1; i < N; i++){
        //fout << "checking: " << delta_copy[i - 1] << endl;
        if(abs(delta_copy[i - 1] - d) >= 2){
            //fout << "failed " << endl;
            fail2 = true;
            count2 = -1;
            //fout << count2 << endl;
            break;
        }
        else if(delta_copy[i - 1] > d){
            delta_copy[i - 1] --;
            delta_copy[i] ++;
            count2++;
        }
        else if(delta_copy[i - 1] < d){
            delta_copy[i - 1] ++;
            delta_copy[i] --;
            count2++;
        }
        else count2 += 0;
        //fout << count2 << endl;
        //fout << endl;
    }
    if(fail2 == false){
        if(delta_copy[N - 2] < d){
            delta_copy[N - 2] ++;
            count2 ++;
        }
        else if(delta_copy[N - 2] > d){
            delta_copy[N - 2] --;
            count2++;
        }
    //fout << count2 << endl;
    }

//testing for diff = e
    int count3 = 0;
    bool fail3 = false;
    //fout << "e = " << e << endl;
    if(delta_c[0] < e){
        delta_c[0] ++;
        count3 ++;
    }
    else if(delta_c[0] > e){
        delta_c[0] --;
        count3++;
    }
    //fout << "before loop " << count1 << endl;
    for(int i = 1; i < N; i++){
        //fout << "checking: " << delta[i - 1] << endl;
        if(abs(delta_c[i - 1] - e) >= 2){
            //fout << "failed" << endl;
            fail3 = true;
            count3 = -1;
            //fout << count3 << endl;
            break;
        }
        else if(delta_c[i - 1] > e){
            delta_c[i - 1] --;
            delta_c[i] ++;
            count3++;
        }
        else if(delta_c[i - 1] < e){
            delta_c[i - 1] ++;
            delta_c[i] --;
            count3++;
        }
        else count3+= 0;
        //fout << count3 << endl;
        //fout << endl;
    }
    if(fail3 == false){
        if(delta_c[N - 2] < e){
            delta_c[N - 2] ++;
            count3 ++;
        }
        else if(delta_c[N - 2] > e){
            delta_c[N - 2] --;
            count3++;
        }
    }
    //fout << count3 << endl;

    //fout << endl;

//fout << "answer: " << endl;
//answer extraction
    //fout << count1 << " " << count2 << " " << count3 << endl;
    int ans[3] = {count1, count2, count3};
    sort(ans, ans + 3);
    bool flag = false;
    for(int i = 0; i < 3; i++){
        if(ans[i] != -1){
            cout << ans[i] << endl;
            flag = true;
            break;
        }
    }
    if(flag == false) cout << -1 << endl;
}


#include <fstream>
#include <iostream>
#include <cmath>
#include <set>
#include <vector>

using namespace std;

int main(){
    //ifstream fin ("test.in");
    //ofstream fout ("test.out");
    int N = 0; cin >> N;
    int seq[N]; int delta[N - 1]; int delta_copy[N - 1]; int delta_c[N - 1]; float avg_delta = 0.0;
    for(int i = 0; i < N; i++){
        cin >> seq[i];
        if(i >= 1){
            delta[i - 1] = seq[i] - seq[i - 1];
            delta_copy[i - 1] = delta[i - 1];
            delta_c[i - 1] = delta[i - 1];
            avg_delta += delta[i - 1];
        }
    }
    avg_delta /= N - 1;
    /*for(int i = 0; i < N; i++){
        fout << seq[i] << " ";
    }fout << endl;*/
    /*for(int i = 0; i < N - 1; i++){
        fout << delta[i] << " ";
    }fout << endl;*/
    int u = ceil(avg_delta); int d = u - 1; int e = u + 1;

//testing for diff = u
    int count1 = 0;
    bool fail1 = false;
    //fout << "u = " << u << endl;
    if(delta[0] < u){
        delta[0] ++;
        count1 ++;
    }
    else if(delta[0] > u){
        delta[0] --;
        count1++;
    }
    //fout << "before loop " << count1 << endl;
    for(int i = 1; i < N; i++){
        //fout << "checking: " << delta[i - 1] << endl;
        if(abs(delta[i - 1] - u) >= 2){
            //fout << "failed" << endl;
            fail1 = true;
            count1 = -1;
            //fout << count1 << endl;
            break;
        }
        else if(delta[i - 1] > u){
            delta[i - 1] --;
            delta[i] ++;
            count1++;
        }
        else if(delta[i - 1] < u){
            delta[i - 1] ++;
            delta[i] --;
            count1++;
        }
        else count1+= 0;
        //fout << count1 << endl;
        //fout << endl;
    }
    if(fail1 == false){
        if(delta[N - 2] < u){
            delta[N - 2] ++;
            count1 ++;
        }
        else if(delta[N - 2] > u){
            delta[N - 2] --;
            count1++;
        }
    }
    //fout << count1 << endl;

    //fout << endl;
//testing for diff = d
    int count2 = 0;
    //fout << "d = " << d << endl;
    if(delta_copy[0] < d){
        delta_copy[0] ++;
        count2 ++;
    }
    else if(delta_copy[0] > d){
        delta_copy[0] --;
        count2++;
    }
    else{
        count2 += 0;
    }
    //fout << "before loop " << count2 << endl;
    bool fail2 = false;
    for(int i = 1; i < N; i++){
        //fout << "checking: " << delta_copy[i - 1] << endl;
        if(abs(delta_copy[i - 1] - d) >= 2){
            //fout << "failed " << endl;
            fail2 = true;
            count2 = -1;
            //fout << count2 << endl;
            break;
        }
        else if(delta_copy[i - 1] > d){
            delta_copy[i - 1] --;
            delta_copy[i] ++;
            count2++;
        }
        else if(delta_copy[i - 1] < d){
            delta_copy[i - 1] ++;
            delta_copy[i] --;
            count2++;
        }
        else count2 += 0;
        //fout << count2 << endl;
        //fout << endl;
    }
    if(fail2 == false){
        if(delta_copy[N - 2] < d){
            delta_copy[N - 2] ++;
            count2 ++;
        }
        else if(delta_copy[N - 2] > d){
            delta_copy[N - 2] --;
            count2++;
        }
    //fout << count2 << endl;
    }

//testing for diff = e
    int count3 = 0;
    bool fail3 = false;
    //fout << "e = " << e << endl;
    if(delta_c[0] < e){
        delta_c[0] ++;
        count3 ++;
    }
    else if(delta_c[0] > e){
        delta_c[0] --;
        count3++;
    }
    //fout << "before loop " << count1 << endl;
    for(int i = 1; i < N; i++){
        //fout << "checking: " << delta[i - 1] << endl;
        if(abs(delta_c[i - 1] - e) >= 2){
            //fout << "failed" << endl;
            fail3 = true;
            count3 = -1;
            //fout << count3 << endl;
            break;
        }
        else if(delta_c[i - 1] > e){
            delta_c[i - 1] --;
            delta_c[i] ++;
            count3++;
        }
        else if(delta_c[i - 1] < e){
            delta_c[i - 1] ++;
            delta_c[i] --;
            count3++;
        }
        else count3+= 0;
        //fout << count3 << endl;
        //fout << endl;
    }
    if(fail3 == false){
        if(delta_c[N - 2] < e){
            delta_c[N - 2] ++;
            count3 ++;
        }
        else if(delta_c[N - 2] > e){
            delta_c[N - 2] --;
            count3++;
        }
    }
    //fout << count3 << endl;

    //fout << endl;

//fout << "answer: " << endl;
//answer extraction
    //fout << count1 << " " << count2 << " " << count3 << endl;
    int ans[3] = {count1, count2, count3};
    sort(ans, ans + 3);
    bool flag = false;
    for(int i = 0; i < 3; i++){
        if(ans[i] != -1){
            cout << ans[i] << endl;
            flag = true;
            break;
        }
    }
    if(flag == false) cout << -1 << endl;
}


#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    //ifstream fin ("test.in");
    //ofstream fout ("test.out");
    int n = 0; cin >> n;
    int seq[n];
    for(int i = 0; i < n; i++){
        seq[i] = 0; cin >> seq[i];
    }
    //idea is to use the possible differences based on first pair
    //then for each run through the sequence
    vector <int> vi;
    for(int i = -1; i <= 1; i++){
        for(int j = -1; j <= 1; j++){
            int count = 0;
            int d = seq[1] - seq[0] + i + j;
            if(abs(d - (seq[1] - seq[0])) == 2) count += 2;
            if(abs(d - (seq[1] - seq[0])) == 1) count += 1;
            //fout << "checking d: " << d << endl;
            //fout <<"pre value of count: " << count << endl;
            int c = 1;
            //fout << "cycling: " << endl;
            while(c < n - 1){
                //fout << " difference: " << seq[c + 1] - (seq[c] + d) << endl;
                if(abs(seq[c + 1] - (seq[c] + d)) < 2){ //can only change 'next term'
                    if(seq[c + 1] - (seq[c] + d) != 0){
                        count ++;
                    }
                    c ++;
                }
                else{
                    count = -1;
                    break;
                }
            }
            if(count != -1) vi.push_back(count);
            //fout << endl;
        }
    }
    if(vi.size() == 0) vi.push_back(-1);
    sort(vi.begin(), vi.end()); cout << vi[0] << endl;
}


#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    //ifstream fin ("test.in");
    //ofstream fout ("test.out");
    int n = 0; cin >> n;
    int seq[n];
    for(int i = 0; i < n; i++){
        seq[i] = 0; cin >> seq[i];
    }
    //idea is to use the possible differences based on first pair
    //then for each run through the sequence
    vector <int> vi;
    for(int i = -1; i <= 1; i++){
        for(int j = -1; j <= 1; j++){
            //fout << "testing pair (i,j): " << i << j << endl;
            int count = 0;
            int d = seq[1] - seq[0] + i + j;
            if(abs(i + j) == 2) count += 2;
            if(abs(i + j) == 1) count += 1;
            int correct_seq[n];
            correct_seq[0] = seq[0] - j; correct_seq[1] = seq[1] + i;
            for(int i = 2; i <= n; i++){
                correct_seq[i] = 0;
                correct_seq[i] = correct_seq[i - 1] + d;
            }
            //fout << "checking d: " << d << endl;
            //fout <<"pre value of count: " << count << endl;
            int c = 2;
            //fout << "cycling: " << endl;
            while(c < n){
                //fout << " difference: " << correct_seq[c] - seq[c] << endl;
                if(abs(correct_seq[c] - seq[c]) < 2){ //can only change 'next term'
                    if(correct_seq[c] - seq[c] != 0){
                        count ++;
                    }
                    c ++;
                }
                else{
                    count = -1;
                    break;
                }
            }
            if(count != -1) vi.push_back(count);
            //fout << "count: " << count << endl;
            //fout << endl;
        }
    }
    if(vi.size() == 0) vi.push_back(-1);
    sort(vi.begin(), vi.end()); cout << vi[0] << endl;
}


#include "bits/stdc++.h"

using namespace std;
using ll = long long;
#define endl '\n'
#define all(v) v.begin(), v.end()

ll check(int d1, int d2, vector<ll> a) {
    a[0] += d1; a[1] += d2;
    ll cnt = abs(d1) + abs(d2);
    ll diff = a[1] - a[0];
    for (int i = 2; i < a.size(); i++) {
       if (a[i] - a[i-1] == diff)
           continue;
       if (a[i] - 1 - a[i-1] == diff) {
           a[i]--;
           cnt++;
       } else if (a[i] + 1 - a[i-1] == diff) {
           a[i]++;
           cnt++;
       } else {
           return 1e9;
       }
    }
    return cnt;
}
int main() {
    int n; cin >> n;
    vector<ll> a(n);
    for (auto& v : a) cin >> v;

    ll res = 1e9;
    for (int d1 = -1; d1 <= 1; d1++)
        for (int d2 = -1; d2 <= 1; d2++)
            res = min(res, check(d1, d2, a));

    if (res == 1e9) res = -1;
    cout << res << '\n';
}


#include "bits/stdc++.h"

using namespace std;
using ll = long long;
#define endl '\n'
#define all(v) v.begin(), v.end()

ll check(int d1, int d2, vector<ll> a) {
    a[0] += d1; a[1] += d2;
    ll cnt = abs(d1) + abs(d2);
    ll diff = a[1] - a[0];
    for (size_t i = 2; i < a.size(); i++) {
       if (a[i] - a[i-1] == diff)
           continue;
       if (a[i] - 1 - a[i-1] == diff) {
           --a[i];
           ++cnt;
       } else if (a[i] + 1 - a[i-1] == diff) {
           ++a[i];
           ++cnt;
       } else {
           return 1e9;
       }
    }
    return cnt;
}
int main() {
    int n; cin >> n;
    vector<ll> a(n);
    for (auto& v : a) cin >> v;

    ll res = 1e9;
    for (int d1 = -1; d1 <= 1; d1++)
        for (int d2 = -1; d2 <= 1; d2++)
            res = min(res, check(d1, d2, a));

    if (res == 1e9) res = -1;
    cout << res << '\n';
}


#include "bits/stdc++.h"

using namespace std;
using ll = long long;
#define endl '\n'
#define all(v) v.begin(), v.end()

ll check(int d1, int d2, vector<ll> a) {
    a[0] += d1; a[1] += d2;
    ll cnt = abs(d1) + abs(d2);
    ll diff = a[1] - a[0];
    for (size_t i = 2; i < a.size(); i++) {
       if (a[i] - a[i-1] == diff)
           continue;
       if (a[i] - 1 - a[i-1] == diff) {
           a[i]--;
           cnt++;
       } else if (a[i] + 1 - a[i-1] == diff) {
           a[i]++;
           cnt++;
       } else {
           return 1e9;
       }
    }
    return cnt;
}
int main() {
    int n; cin >> n;
    vector<ll> a(n);
    for (auto& v : a) cin >> v;

    ll res = 1e9;
    for (int d1 = -1; d1 <= 1; d1++)
        for (int d2 = -1; d2 <= 1; d2++)
            res = min(res, check(d1, d2, a));

    if (res == 1e9) res = -1;
    cout << res << '\n';
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
typedef  vector<int> vi;
typedef  pair<int,int> pii;
typedef  pair<ll,int> pli;
typedef  pair<int,string> pis;
double   pi=3.1415926535898;
#define speed      ios::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define lop(i,n)    for(int i=0;i<int(n);i++)
#define ff(i,a,b)  for(int i=a;i<=int(b);++i)
#define fr(i,a,b)  for(int i=a;i>=int(b);i--)
#define ms(x,a)    memset(x,a,sizeof(x))
#define all(x)     (x).begin(),(x).end()
#define sz(x)      int(x.size())
#define pb         push_back
#define r0         return 0;
#define CC         continue;
#define bb(x)      cerr<< x <<endl;
#define f          first
//#define s          second       //  __builtin_popcount;
//freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
string ys = "YES\n";
string no = "NO\n" ;
const double EPS = 1e-6;
const int inf = 1<<20;
const ll M  = 1e9+7 ;
const int N = 100100;
const int maxn = 105;
/////  for (int i= -inf ; i < inf ; i++)   Hi  :P

ll n,w;
ll ans,c, d;
ll a[100100];
ll b[100100];
//int mx[]= {0,0,1,-1, 1,1,-1,-1};
//int my[]= {1,-1,0,0, 1,-1,1,-1};

int mx[]= {0,0,0,1,1,1,-1,-1,-1};
int my[]= {0,1,-1,0,1,-1,0,1,-1};

int main()
{
    speed;
    cin >> n;
    ff(i,1,n)
    {
        cin >> a[i];
    }

    if (n <= 2LL)
    {
        cout << 0 << endl;
        return 0;
    }

    ans = 1e9;
    ff(k,0,7)
    {
        ff(i,1,n) b[i] = a[i];

        b[1] += mx[k];
        b[2] += my[k];

        d = b[1]-b[2];

        c = abs(mx[k])+abs(my[k]);

        ff(i,3,n)
        {
            if (b[i-1] - b[i] != d)
            {
                if (b[i-1]- (b[i]+1LL) == d)
                {
                    b[i]++;
                    c++;
                }
                else if (b[i-1]- (b[i]-1LL) == d)
                {
                    b[i]--;
                    c++;
                }
                else
                {
                    c = 1e9;
                    break;
                }
            }
        }
        ans = min(ans,c);
    }
    if (ans == 1e9)
    {
        ans = -1LL;
    }
    cout<< ans<<endl;
}

/*
int mx[]= {0,0,1,-1, 1,1,-1,-1};
int my[]= {1,-1,0,0, 1,-1,1,-1};
bool inside (int i,int j){return (i>=0 && i<n && j>=0 && j<m);}

ll BIT[100100];
void update(int x, int delta)
{
    for(; x <= N; x += x&-x)
        BIT[x] += delta;
}
ll query(int x)
{
    ll sum = 0;
    for(; x > 0; x -= x&-x)
        sum += BIT[x];
    return sum;
}

bool prime[1000005];
void sieve()
{
    for(int i=4; i<=1000000; i+=2)
        prime[i]=false,prime[i-1]=true;
    prime[2]=true;
    for(int i=3; i<=1000; i+=2)
    {
        if(prime[i])
        {
            for(int j=i*i; j<=1000000; j+=i*2)
                prime[j]=false;
        }
    }
}

for (int i=0; i<n; i++)            Hi    ^_^  hack me if you can :P
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

*/
/*
freopen("output.txt","w",stdout);
freopen("input.txt","r",stdin);

ll pwmod(ll a, ll n, ll mod)
{
ll ret = 1;
while (n)
{
if (n & 1) ret = ret * a % mod;
a = a * a % mod;
n >>= 1;
}
return ret;
}

void gcd(LL n,LL m,LL &d,LL &xx,LL &yy)
{
if(!m)
{
d=n;
xx=1;
yy=0;
}
else
{
gcd(m,n%m,d,yy,xx);
yy-=xx*(n/m);
}
}

int getm(int l,int r)
{
if(l>r)
return N;
if(l==r)
return 0;
if(l==r-1)
return s[l] != s[r];
if(s[l]==s[r])
return getm(l+1,r-1);
else
return 1+min(getm(l+1,r),getm(l,r-1));
}

int n, a[N];
int seg[N<<2];

void build(int nd,int L,int R)
{
if( L == R )
{
seg[nd]=a[L];
return ;
}
int mid = (L+R) >>1;
build(nd<<1,L, mid);
build(nd<<1|1,mid+1,R);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

void update(int nd,int L,int R,int idx,int val)
{
if( L == R )
{
seg[nd]=val;
return ;
}
int mid = (L+R) >>1;
if( idx<=mid )
update(nd<<1,  L,mid,idx,val);
else update(nd<<1|1,mid+1, R,idx,val);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

int query(int nd,int L,int R,int from,int to)
{
if( from<=L && R<=to )return seg[nd];
if( from>R || to<L )return -inf;

int mid = (L+R) >>1;

int ansl = query(nd<<1,  L,mid,from,to);
int ansr= query(nd<<1|1,mid+1, R,from,to);
return max( anrl,ansr );
}
*/


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
typedef  vector<int> vi;
typedef  pair<int,int> pii;
typedef  pair<ll,int> pli;
typedef  pair<int,string> pis;
double   pi=3.1415926535898;
#define speed      ios::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define lop(i,n)    for(int i=0;i<int(n);i++)
#define ff(i,a,b)  for(int i=a;i<=int(b);++i)
#define fr(i,a,b)  for(int i=a;i>=int(b);i--)
#define ms(x,a)    memset(x,a,sizeof(x))
#define all(x)     (x).begin(),(x).end()
#define sz(x)      int(x.size())
#define pb         push_back
#define r0         return 0;
#define CC         continue;
#define bb(x)      cerr<< x <<endl;
#define f          first
//#define s          second       //  __builtin_popcount;
//freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
string ys = "YES\n";
string no = "NO\n" ;
const double EPS = 1e-6;
const int inf = 1<<20;
const ll M  = 1e9+7 ;
const int N = 100100;
const int maxn = 105;
//////////////////////  Hi ^_^  hack me if you can :P

ll n,w;
ll ans,c, d;
ll a[100100];
ll b[100100];
//int mx[]= {0,0,1,-1, 1,1,-1,-1};
//int my[]= {1,-1,0,0, 1,-1,1,-1};

int mx[]= {0,0,0,1,1,1,-1,-1,-1};
int my[]= {0,1,-1,0,1,-1,0,1,-1};

int main()
{
    speed;
    cin >> n;
    ff(i,1,n)
    {
        cin >> a[i];
    }

    if (n <= 2LL)
    {
        cout << 0 << endl;
        return 0;
    }

    ans = n+5LL;
    ff(k,0,7)
    {
        ff(i,1,n) b[i] = a[i];

        b[1] += mx[k];
        b[2] += my[k];

        d = b[1]-b[2];

        c = abs(mx[k])+abs(my[k]);

        ff(i,3,n)
        {
            if (b[i-1] - b[i] != d)
            {
                if (b[i-1]- (b[i]+1LL) == d)
                {
                    b[i]++;
                    c++;
                }
                else if (b[i-1]- (b[i]-1LL) == d)
                {
                    b[i]--;
                    c++;
                }
                else
                {
                    c = n+5LL;
                    break;
                }
            }
        }
        ans = min(ans,c);
    }
    if (ans == n+5LL)
    {
        ans = -1LL;
    }
    cout<< ans<<endl;
}

/*

ll BIT[100100];

void update(int x, int delta)
{
    for(; x <= N; x += x&-x)
        BIT[x] += delta;
}

ll query(int x)
{
    ll sum = 0;
    for(; x > 0; x -= x&-x)
        sum += BIT[x];
    return sum;
}

bool prime[1000005];
void sieve()
{
    for(int i=4; i<=1000000; i+=2)
        prime[i]=false,prime[i-1]=true;
    prime[2]=true;
    for(int i=3; i<=1000; i+=2)
    {
        if(prime[i])
        {
            for(int j=i*i; j<=1000000; j+=i*2)
                prime[j]=false;
        }
    }
}

for (int i=0; i<n; i++)            Hi    ^_^  hack me if you can :P
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

*/
/*
freopen("output.txt","w",stdout);
freopen("input.txt","r",stdin);

ll pwmod(ll a, ll n, ll mod)
{
ll ret = 1;
while (n)
{
if (n & 1) ret = ret * a % mod;
a = a * a % mod;
n >>= 1;
}
return ret;
}

int mx[]= {0,0,1,-1, 1,1,-1,-1};
int my[]= {1,-1,0,0, 1,-1,1,-1};

bool inside (int i,int j){return (i>=0 && i<n && j>=0 && j<m);}

void gcd(LL n,LL m,LL &d,LL &xx,LL &yy)
{
if(!m)
{
d=n;
xx=1;
yy=0;
}
else
{
gcd(m,n%m,d,yy,xx);
yy-=xx*(n/m);
}
}

int getm(int l,int r)
{
if(l>r)
return N;
if(l==r)
return 0;
if(l==r-1)
return s[l] != s[r];
if(s[l]==s[r])
return getm(l+1,r-1);
else
return 1+min(getm(l+1,r),getm(l,r-1));
}

int n, a[N];
int seg[N<<2];

void build(int nd,int L,int R)
{
if( L == R )
{
seg[nd]=a[L];
return ;
}
int mid = (L+R) >>1;
build(nd<<1,L, mid);
build(nd<<1|1,mid+1,R);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

void update(int nd,int L,int R,int idx,int val)
{
if( L == R )
{
seg[nd]=val;
return ;
}
int mid = (L+R) >>1;
if( idx<=mid )
update(nd<<1,  L,mid,idx,val);
else update(nd<<1|1,mid+1, R,idx,val);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

int query(int nd,int L,int R,int from,int to)
{
if( from<=L && R<=to )return seg[nd];
if( from>R || to<L )return -inf;

int mid = (L+R) >>1;

int ansl = query(nd<<1,  L,mid,from,to);
int ansr= query(nd<<1|1,mid+1, R,from,to);
return max( anrl,ansr );
}
*/


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
typedef  vector<int> vi;
typedef  pair<int,int> pii;
typedef  pair<ll,int> pli;
typedef  pair<int,string> pis;
double   pi=3.1415926535898;
#define speed      ios::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define lop(i,n)    for(int i=0;i<int(n);i++)
#define ff(i,a,b)  for(int i=a;i<=int(b);++i)
#define fr(i,a,b)  for(int i=a;i>=int(b);i--)
#define ms(x,a)    memset(x,a,sizeof(x))
#define all(x)     (x).begin(),(x).end()
#define sz(x)      int(x.size())
#define pb         push_back
#define r0         return 0;
#define CC         continue;
#define bb(x)      cerr<< x <<endl;
#define f          first
//#define s          second       //  __builtin_popcount;
//freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
string ys = "YES\n";
string no = "NO\n" ;
const double EPS = 1e-6;
const int inf = 1<<20;
const ll M  = 1e9+7 ;
const int N = 100100;
const int maxn = 105;
//////////////////////  Hi ^_^  hack me if you can :P

ll n,w;
ll ans,c, d;
ll a[100100];
ll b[100100];
//int mx[]= {0,0,1,-1, 1,1,-1,-1};
//int my[]= {1,-1,0,0, 1,-1,1,-1};

int mx[]= {0,0,0,1,1,1,-1,-1,-1};
int my[]= {0,1,-1,0,1,-1,0,1,-1};

int main()
{
    speed;
    cin >> n;
    ff(i,1,n)
    {
        cin >> a[i];
    }

    if (n <= 2LL)
    {
        cout << 0 << endl;
        return 0;
    }

    ans = 1e9;
    ff(k,0,7)
    {
        ff(i,1,n) b[i] = a[i];

        b[1] += mx[k];
        b[2] += my[k];

        d = b[1]-b[2];

        c = abs(mx[k])+abs(my[k]);

        ff(i,3,n)
        {
            if (b[i-1] - b[i] != d)
            {
                if (b[i-1]- (b[i]+1LL) == d)
                {
                    b[i]++;
                    c++;
                }
                else if (b[i-1]- (b[i]-1LL) == d)
                {
                    b[i]--;
                    c++;
                }
                else
                {
                    c = 1e9;
                    break;
                }
            }
        }
        ans = min(ans,c);
    }
    if (ans == 1e9)
    {
        ans = -1LL;
    }
    cout<< ans<<endl;
}

/*

ll BIT[100100];

void update(int x, int delta)
{
    for(; x <= N; x += x&-x)
        BIT[x] += delta;
}

ll query(int x)
{
    ll sum = 0;
    for(; x > 0; x -= x&-x)
        sum += BIT[x];
    return sum;
}

bool prime[1000005];
void sieve()
{
    for(int i=4; i<=1000000; i+=2)
        prime[i]=false,prime[i-1]=true;
    prime[2]=true;
    for(int i=3; i<=1000; i+=2)
    {
        if(prime[i])
        {
            for(int j=i*i; j<=1000000; j+=i*2)
                prime[j]=false;
        }
    }
}

for (int i=0; i<n; i++)            Hi    ^_^  hack me if you can :P
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

*/
/*
freopen("output.txt","w",stdout);
freopen("input.txt","r",stdin);

ll pwmod(ll a, ll n, ll mod)
{
ll ret = 1;
while (n)
{
if (n & 1) ret = ret * a % mod;
a = a * a % mod;
n >>= 1;
}
return ret;
}

int mx[]= {0,0,1,-1, 1,1,-1,-1};
int my[]= {1,-1,0,0, 1,-1,1,-1};

bool inside (int i,int j){return (i>=0 && i<n && j>=0 && j<m);}

void gcd(LL n,LL m,LL &d,LL &xx,LL &yy)
{
if(!m)
{
d=n;
xx=1;
yy=0;
}
else
{
gcd(m,n%m,d,yy,xx);
yy-=xx*(n/m);
}
}

int getm(int l,int r)
{
if(l>r)
return N;
if(l==r)
return 0;
if(l==r-1)
return s[l] != s[r];
if(s[l]==s[r])
return getm(l+1,r-1);
else
return 1+min(getm(l+1,r),getm(l,r-1));
}

int n, a[N];
int seg[N<<2];

void build(int nd,int L,int R)
{
if( L == R )
{
seg[nd]=a[L];
return ;
}
int mid = (L+R) >>1;
build(nd<<1,L, mid);
build(nd<<1|1,mid+1,R);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

void update(int nd,int L,int R,int idx,int val)
{
if( L == R )
{
seg[nd]=val;
return ;
}
int mid = (L+R) >>1;
if( idx<=mid )
update(nd<<1,  L,mid,idx,val);
else update(nd<<1|1,mid+1, R,idx,val);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

int query(int nd,int L,int R,int from,int to)
{
if( from<=L && R<=to )return seg[nd];
if( from>R || to<L )return -inf;

int mid = (L+R) >>1;

int ansl = query(nd<<1,  L,mid,from,to);
int ansr= query(nd<<1|1,mid+1, R,from,to);
return max( anrl,ansr );
}
*/


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
typedef  vector<int> vi;
typedef  pair<int,int> pii;
typedef  pair<ll,int> pli;
typedef  pair<int,string> pis;
double   pi=3.1415926535898;
#define speed      ios::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define lop(i,n)    for(int i=0;i<int(n);i++)
#define ff(i,a,b)  for(int i=a;i<=int(b);++i)
#define fr(i,a,b)  for(int i=a;i>=int(b);i--)
#define ms(x,a)    memset(x,a,sizeof(x))
#define all(x)     (x).begin(),(x).end()
#define sz(x)      int(x.size())
#define pb         push_back
#define r0         return 0;
#define CC         continue;
#define bb(x)      cerr<< x <<endl;
#define f          first
//#define s          second       //  __builtin_popcount;
//freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
string ys = "YES\n";
string no = "NO\n" ;
const double EPS = 1e-6;
const int inf = 1<<20;
const ll M  = 1e9+7 ;
const int N = 100100;
const int maxn = 105;
/////  for (int i= -inf ; i < inf ; i++)   Hi  :P

ll n,w;
ll ans,c, d;
ll a[100100];
ll b[100100];
//int mx[]= {0,0,1,-1, 1,1,-1,-1};
//int my[]= {1,-1,0,0, 1,-1,1,-1};

int mx[]= {0,0,0,1,1,1,-1,-1,-1};
int my[]= {0,1,-1,0,1,-1,0,1,-1};

int main()
{
    speed;
    cin >> n;
    ff(i,1,n)
    {
        cin >> a[i];
    }

    if (n <= 2LL)
    {
        cout << 0 << endl;
        return 0;
    }

    ans = 1e9;
    ff(k,0,8)
    {
        ff(i,1,n) b[i] = a[i];

        b[1] += mx[k];
        b[2] += my[k];

        d = b[1]-b[2];

        c = abs(mx[k])+abs(my[k]);

        ff(i,3,n)
        {
            if (b[i-1] - b[i] != d)
            {
                if (b[i-1]- (b[i]+1LL) == d)
                {
                    b[i]++;
                    c++;
                }
                else if (b[i-1]- (b[i]-1LL) == d)
                {
                    b[i]--;
                    c++;
                }
                else
                {
                    c = 1e9;
                    break;
                }
            }
        }
        ans = min(ans,c);
    }
    if (ans == 1e9)
    {
        ans = -1LL;
    }
    cout<< ans<<endl;
}

/*
int mx[]= {0,0,1,-1, 1,1,-1,-1};
int my[]= {1,-1,0,0, 1,-1,1,-1};
bool inside (int i,int j){return (i>=0 && i<n && j>=0 && j<m);}

ll BIT[100100];
void update(int x, int delta)
{
    for(; x <= N; x += x&-x)
        BIT[x] += delta;
}
ll query(int x)
{
    ll sum = 0;
    for(; x > 0; x -= x&-x)
        sum += BIT[x];
    return sum;
}

bool prime[1000005];
void sieve()
{
    for(int i=4; i<=1000000; i+=2)
        prime[i]=false,prime[i-1]=true;
    prime[2]=true;
    for(int i=3; i<=1000; i+=2)
    {
        if(prime[i])
        {
            for(int j=i*i; j<=1000000; j+=i*2)
                prime[j]=false;
        }
    }
}

for (int i=0; i<n; i++)            Hi    ^_^  hack me if you can :P
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

*/
/*
freopen("output.txt","w",stdout);
freopen("input.txt","r",stdin);

ll pwmod(ll a, ll n, ll mod)
{
ll ret = 1;
while (n)
{
if (n & 1) ret = ret * a % mod;
a = a * a % mod;
n >>= 1;
}
return ret;
}

void gcd(LL n,LL m,LL &d,LL &xx,LL &yy)
{
if(!m)
{
d=n;
xx=1;
yy=0;
}
else
{
gcd(m,n%m,d,yy,xx);
yy-=xx*(n/m);
}
}

int getm(int l,int r)
{
if(l>r)
return N;
if(l==r)
return 0;
if(l==r-1)
return s[l] != s[r];
if(s[l]==s[r])
return getm(l+1,r-1);
else
return 1+min(getm(l+1,r),getm(l,r-1));
}

int n, a[N];
int seg[N<<2];

void build(int nd,int L,int R)
{
if( L == R )
{
seg[nd]=a[L];
return ;
}
int mid = (L+R) >>1;
build(nd<<1,L, mid);
build(nd<<1|1,mid+1,R);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

void update(int nd,int L,int R,int idx,int val)
{
if( L == R )
{
seg[nd]=val;
return ;
}
int mid = (L+R) >>1;
if( idx<=mid )
update(nd<<1,  L,mid,idx,val);
else update(nd<<1|1,mid+1, R,idx,val);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

int query(int nd,int L,int R,int from,int to)
{
if( from<=L && R<=to )return seg[nd];
if( from>R || to<L )return -inf;

int mid = (L+R) >>1;

int ansl = query(nd<<1,  L,mid,from,to);
int ansr= query(nd<<1|1,mid+1, R,from,to);
return max( anrl,ansr );
}
*/


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
typedef  vector<int> vi;
typedef  pair<int,int> pii;
typedef  pair<ll,int> pli;
typedef  pair<int,string> pis;
double   pi=3.1415926535898;
#define speed      ios::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define lop(i,n)    for(int i=0;i<int(n);i++)
#define ff(i,a,b)  for(int i=a;i<=int(b);++i)
#define fr(i,a,b)  for(int i=a;i>=int(b);i--)
#define ms(x,a)    memset(x,a,sizeof(x))
#define all(x)     (x).begin(),(x).end()
#define sz(x)      int(x.size())
#define pb         push_back
#define r0         return 0;
#define CC         continue;
#define bb(x)      cerr<< x <<endl;
#define f          first
//#define s          second       //  __builtin_popcount;
//freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
string ys = "YES\n";
string no = "NO\n" ;
const double EPS = 1e-6;
const int inf = 1<<20;
const ll M  = 1e9+7 ;
const int N = 100100;
const int maxn = 105;
//////////////////////  Hi ^_^  hack me if you can :P

ll n,w;
ll ans,c, d;
ll a[100100];
ll b[100100];
//int mx[]= {0,0,1,-1, 1,1,-1,-1};
//int my[]= {1,-1,0,0, 1,-1,1,-1};

int mx[]= {0,0,0,1,1,1,-1,-1,-1};
int my[]= {0,1,-1,0,1,-1,0,1,-1};

int main()
{
    speed;
    cin >> n;
    ff(i,1,n)
    {
        cin >> a[i];
    }

    if (n <= 2LL)
    {
        cout << 0 << endl;
        return 0;
    }

    ans = n+5LL;
    ff(k,0,7)
    {
        ff(i,1,n) b[i] = a[i];

        b[1] += mx[k];
        b[2] += my[k];

        d = b[1]-b[2];

        c = abs(mx[k])+abs(my[k]);

        ff(i,3,n)
        {
            if (b[i-1] - b[i] != d)
            {
                if (b[i-1]-b[i] == d+1LL)
                {
                    b[i]++;
                    c++;
                }
                else if (b[i-1]-b[i] == d-1LL)
                {
                    b[i]--;
                    c++;
                }
                else
                {
                    c = n+1LL;
                    break;
                }
            }
        }
        ans = min(ans,c);
    }
    if (ans == n+5LL)
    {
        ans = -1LL;
    }
    cout<< ans<<endl;
}

/*

ll BIT[100100];

void update(int x, int delta)
{
    for(; x <= N; x += x&-x)
        BIT[x] += delta;
}

ll query(int x)
{
    ll sum = 0;
    for(; x > 0; x -= x&-x)
        sum += BIT[x];
    return sum;
}

bool prime[1000005];
void sieve()
{
    for(int i=4; i<=1000000; i+=2)
        prime[i]=false,prime[i-1]=true;
    prime[2]=true;
    for(int i=3; i<=1000; i+=2)
    {
        if(prime[i])
        {
            for(int j=i*i; j<=1000000; j+=i*2)
                prime[j]=false;
        }
    }
}

for (int i=0; i<n; i++)            Hi    ^_^  hack me if you can :P
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

*/
/*
freopen("output.txt","w",stdout);
freopen("input.txt","r",stdin);

ll pwmod(ll a, ll n, ll mod)
{
ll ret = 1;
while (n)
{
if (n & 1) ret = ret * a % mod;
a = a * a % mod;
n >>= 1;
}
return ret;
}

int mx[]= {0,0,1,-1, 1,1,-1,-1};
int my[]= {1,-1,0,0, 1,-1,1,-1};

bool inside (int i,int j){return (i>=0 && i<n && j>=0 && j<m);}

void gcd(LL n,LL m,LL &d,LL &xx,LL &yy)
{
if(!m)
{
d=n;
xx=1;
yy=0;
}
else
{
gcd(m,n%m,d,yy,xx);
yy-=xx*(n/m);
}
}

int getm(int l,int r)
{
if(l>r)
return N;
if(l==r)
return 0;
if(l==r-1)
return s[l] != s[r];
if(s[l]==s[r])
return getm(l+1,r-1);
else
return 1+min(getm(l+1,r),getm(l,r-1));
}

int n, a[N];
int seg[N<<2];

void build(int nd,int L,int R)
{
if( L == R )
{
seg[nd]=a[L];
return ;
}
int mid = (L+R) >>1;
build(nd<<1,L, mid);
build(nd<<1|1,mid+1,R);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

void update(int nd,int L,int R,int idx,int val)
{
if( L == R )
{
seg[nd]=val;
return ;
}
int mid = (L+R) >>1;
if( idx<=mid )
update(nd<<1,  L,mid,idx,val);
else update(nd<<1|1,mid+1, R,idx,val);
seg[nd]=max( seg[nd<<1], seg[nd<<1|1] );
}

int query(int nd,int L,int R,int from,int to)
{
if( from<=L && R<=to )return seg[nd];
if( from>R || to<L )return -inf;

int mid = (L+R) >>1;

int ansl = query(nd<<1,  L,mid,from,to);
int ansr= query(nd<<1|1,mid+1, R,from,to);
return max( anrl,ansr );
}
*/