#include<bits/stdc++.h> // 68 79 "Baklol"
using namespace std;

#define rep(x, k, n)    for(int x = (k); x < (n); ++x)
#define repr(x, k, n)   for(int x = (k); x > (n); --x)
#define tr(it, a)       for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll              long long
#define siz(x)          ((int)(x).size())
#define print(x)        cout << (x) << '\n'
#define prints(x)       cout << (x) << " "
#define eb              emplace_back
#define mp              make_pair
#define F               first
#define S               second
#define o2(x)           ( (x) * (x) )
#define all(x)          (x).begin(), (x).end()
#define allr(x)         (x).rbegin(), (x).rend()
#define clr(x, k)       memset(x, k, sizeof(x))
#define sortall(x)      sort(all(x))
#define sortalld(x)     sort(all(x), greater<int>())
#define printv(v)       rep(x, 0, siz(v)){ prints(v[x]); } cout << '\n'
#define print2dv(V)     rep(y, 0, siz(V)){ printv(V[y]); }
#define out(x)          cout << ((x) ? "Yes" : "No") << '\n';
#define inf             INT_MAX
#define ninf            INT_MIN
#define int             long long // take care of it when too much computation required

typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<bool>    vb;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
typedef vector<vb>      vvb;

template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
    cout << name << " = " << arg1  << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " = " << arg1 <<",";
    debug_out(comma+1, args...);
}

#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const int mod = 1e9+7; // 998244353
const int N = 3e5, M = N;

/*
-> Do not forget to check the edge cases.
-> Do something instead of nothing.
-> Make sure you are submitting the right file.
-> Do not make "easy->complicated", xd... :|.
*/

//-----------------------------------------------------------------------------

int check(int diff, vi temp){
    vi v = temp;
    int res = inf, cnt = 0;
    int n = siz(v);
    rep(i, 1, n){
        int d = v[i] - v[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]--;
                    v[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]--;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]++;
                    v[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]++;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    cnt = 0;
    repr(i, n-1, 0){
        int d = temp[i] - temp[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]--;
                    temp[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]++;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]++;
                    temp[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]--;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    return res;
}

void Solve_main() {

    int n;
    cin >> n;
    vi v(n);
    rep(i, 0, n){
        cin >> v[i];
    }
    if(n == 1){
        cout << 0 << '\n';
        return;
    }
    if(v.back() < v.front()){
        reverse(all(v));
    }
    int diffsum = 0;
    rep(i, 1, n){
        diffsum += v[i] - v[i-1];
    }

    int diff1 = diffsum / (n - 1);
    int diff2 = (diffsum + n - 2) / (n - 1);
    int diff = abs(diffsum - diff1 * (n - 1)) > abs(diffsum - diff2 * (n - 1)) ? diff2 : diff1;
    // int res = check(diff2, v);
    int res = check(diff, v);
    // res = min(res, check(diff1, v));
    if(res >= inf){
        cout << -1 << '\n';
    }
    else{
        cout << res << '\n';
    }

}

//-----------------------------------------------------------------------------

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    // cout << setprecision(12) << fixed;
    int tc = 1;
    // cin >> tc;
    for(int i = 0; i < tc; i++) {
        Solve_main();
    }

    cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
    return 0;
}


#include<bits/stdc++.h> // 68 79 "Baklol"
using namespace std;

#define rep(x, k, n)    for(int x = (k); x < (n); ++x)
#define repr(x, k, n)   for(int x = (k); x > (n); --x)
#define tr(it, a)       for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll              long long
#define siz(x)          ((int)(x).size())
#define print(x)        cout << (x) << '\n'
#define prints(x)       cout << (x) << " "
#define eb              emplace_back
#define mp              make_pair
#define F               first
#define S               second
#define o2(x)           ( (x) * (x) )
#define all(x)          (x).begin(), (x).end()
#define allr(x)         (x).rbegin(), (x).rend()
#define clr(x, k)       memset(x, k, sizeof(x))
#define sortall(x)      sort(all(x))
#define sortalld(x)     sort(all(x), greater<int>())
#define printv(v)       rep(x, 0, siz(v)){ prints(v[x]); } cout << '\n'
#define print2dv(V)     rep(y, 0, siz(V)){ printv(V[y]); }
#define out(x)          cout << ((x) ? "Yes" : "No") << '\n';
#define inf             INT_MAX
#define ninf            INT_MIN
#define int             long long // take care of it when too much computation required

typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<bool>    vb;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
typedef vector<vb>      vvb;

template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
    cout << name << " = " << arg1  << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " = " << arg1 <<",";
    debug_out(comma+1, args...);
}

#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const int mod = 1e9+7; // 998244353
const int N = 3e5, M = N;

/*
-> Do not forget to check the edge cases.
-> Do something instead of nothing.
-> Make sure you are submitting the right file.
-> Do not make "easy->complicated", xd... :|.
*/

//-----------------------------------------------------------------------------

int check(int diff, vi temp){
    vi v = temp;
    int res = inf, cnt = 0;
    int n = siz(v);
    rep(i, 1, n){
        int d = v[i] - v[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]--;
                    v[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]--;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]++;
                    v[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]++;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    cnt = 0;
    repr(i, n-1, 0){
        int d = temp[i] - temp[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]--;
                    temp[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]++;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]++;
                    temp[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]--;
                cnt++;
            }
        }
    }
    // deb(res, cnt);
    res = min(res, cnt);
    // deb(diff, res);
    return res;
}

void Solve_main() {

    int n;
    cin >> n;
    vi v(n);
    rep(i, 0, n){
        cin >> v[i];
    }
    if(n == 1){
        cout << 0 << '\n';
        return;
    }
    if(v.back() < v.front()){
        reverse(all(v));
    }
    // printv(v);
    int diffsum = 0;
    rep(i, 1, n){
        diffsum += v[i] - v[i-1];
        // deb(v[i] - v[i-1]);
    }
    // deb(diffsum);
    int diff1 = diffsum / (n - 1);
    int diff2 = (diffsum + n - 2) / (n - 1);
    // deb(diff1, diff2);
    int res = check(diff2, v);
    res = min(res, check(diff1, v));
    if(res >= inf){
        cout << -1 << '\n';
    }
    else{
        cout << res << '\n';
    }

}

//-----------------------------------------------------------------------------

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    // cout << setprecision(12) << fixed;
    int tc = 1;
    // cin >> tc;
    for(int i = 0; i < tc; i++) {
        Solve_main();
    }

    cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
    return 0;
}


#include<bits/stdc++.h> // 68 79 "Baklol"
using namespace std;

#define rep(x, k, n)    for(int x = (k); x < (n); ++x)
#define repr(x, k, n)   for(int x = (k); x > (n); --x)
#define tr(it, a)       for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll              long long
#define siz(x)          ((int)(x).size())
#define print(x)        cout << (x) << '\n'
#define prints(x)       cout << (x) << " "
#define eb              emplace_back
#define mp              make_pair
#define F               first
#define S               second
#define o2(x)           ( (x) * (x) )
#define all(x)          (x).begin(), (x).end()
#define allr(x)         (x).rbegin(), (x).rend()
#define clr(x, k)       memset(x, k, sizeof(x))
#define sortall(x)      sort(all(x))
#define sortalld(x)     sort(all(x), greater<int>())
#define printv(v)       rep(x, 0, siz(v)){ prints(v[x]); } cout << '\n'
#define print2dv(V)     rep(y, 0, siz(V)){ printv(V[y]); }
#define out(x)          cout << ((x) ? "Yes" : "No") << '\n';
#define inf             INT_MAX
#define ninf            INT_MIN
#define int             long long // take care of it when too much computation required

typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<bool>    vb;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
typedef vector<vb>      vvb;

template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
    cout << name << " = " << arg1  << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " = " << arg1 <<",";
    debug_out(comma+1, args...);
}

#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const int mod = 1e9+7; // 998244353
const int N = 3e5, M = N;

/*
-> Do not forget to check the edge cases.
-> Do something instead of nothing.
-> Make sure you are submitting the right file.
-> Do not make "easy->complicated", xd... :|.
*/

//-----------------------------------------------------------------------------

int check(int diff, vi temp){
    vi v = temp;
    int res = inf, cnt = 0;
    int n = siz(v);
    rep(i, 1, n){
        int d = v[i] - v[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]--;
                    v[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]--;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]++;
                    v[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]++;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    cnt = 0;
    repr(i, n-1, 0){
        int d = temp[i] - temp[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]--;
                    temp[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]++;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]++;
                    temp[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]--;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    return res;
}

void redundent(){
    int num = 1e9;
    int res = 1;
    while(num--){
        res += res;
        deb(num);
    }
}

void Solve_main() {
    redundent();
    int n;
    cin >> n;
    vi v(n);
    rep(i, 0, n){
        cin >> v[i];
    }
    if(n == 1){
        cout << 0 << '\n';
        return;
    }
    if(v.back() < v.front()){
        reverse(all(v));
    }
    int diffsum = 0;
    rep(i, 1, n){
        diffsum += v[i] - v[i-1];
    }

    int diff1 = diffsum / (n - 1);
    int diff2 = (diffsum + n - 2) / (n - 1);
    int res = check(diff2, v);
    res = min(res, check(diff1, v));
    if(res >= inf){
        cout << -1 << '\n';
    }
    else{
        cout << res << '\n';
    }
}

//-----------------------------------------------------------------------------

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    // cout << setprecision(12) << fixed;
    int tc = 1;
    // cin >> tc;
    for(int i = 0; i < tc; i++) {
        Solve_main();
    }

    cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
    return 0;
}


#include<bits/stdc++.h> // 68 79 "Baklol"
using namespace std;

#define rep(x, k, n)    for(int x = (k); x < (n); ++x)
#define repr(x, k, n)   for(int x = (k); x > (n); --x)
#define tr(it, a)       for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll              long long
#define siz(x)          ((int)(x).size())
#define print(x)        cout << (x) << '\n'
#define prints(x)       cout << (x) << " "
#define eb              emplace_back
#define mp              make_pair
#define F               first
#define S               second
#define o2(x)           ( (x) * (x) )
#define all(x)          (x).begin(), (x).end()
#define allr(x)         (x).rbegin(), (x).rend()
#define clr(x, k)       memset(x, k, sizeof(x))
#define sortall(x)      sort(all(x))
#define sortalld(x)     sort(all(x), greater<int>())
#define printv(v)       rep(x, 0, siz(v)){ prints(v[x]); } cout << '\n'
#define print2dv(V)     rep(y, 0, siz(V)){ printv(V[y]); }
#define out(x)          cout << ((x) ? "Yes" : "No") << '\n';
#define inf             INT_MAX
#define ninf            INT_MIN
#define int             long long // take care of it when too much computation required

typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<bool>    vb;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
typedef vector<vb>      vvb;

template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
    cout << name << " = " << arg1  << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " = " << arg1 <<",";
    debug_out(comma+1, args...);
}

#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const int mod = 1e9+7; // 998244353
const int N = 3e5, M = N;

/*
-> Do not forget to check the edge cases.
-> Do something instead of nothing.
-> Make sure you are submitting the right file.
-> Do not make "easy->complicated", xd... :|.
*/

//-----------------------------------------------------------------------------

int check(int diff, vi temp){
    vi v = temp;
    int res = inf, cnt = 0;
    int n = siz(v);
    rep(i, 1, n){
        int d = v[i] - v[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]--;
                    v[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]--;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]++;
                    v[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]++;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    cnt = 0;
    repr(i, n-1, 0){
        int d = temp[i] - temp[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]--;
                    temp[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]++;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]++;
                    temp[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]--;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    return res;
}

void Solve_main() {

    int n;
    cin >> n;
    vi v(n);
    rep(i, 0, n){
        cin >> v[i];
    }
    if(n == 1){
        cout << 0 << '\n';
        return;
    }
    if(v.back() < v.front()){
        reverse(all(v));
    }
    int diffsum = 0;
    rep(i, 1, n){
        diffsum += v[i] - v[i-1];
    }

    int diff1 = diffsum / (n - 1);
    int diff2 = (diffsum + n - 2) / (n - 1);
    int res = check(diff2, v);
    res = min(res, check(diff1, v));
    if(res >= inf){
        cout << -1 << '\n';
    }
    else{
        cout << res << '\n';
    }

}

//-----------------------------------------------------------------------------

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    // cout << setprecision(12) << fixed;
    int tc = 1;
    // cin >> tc;
    for(int i = 0; i < tc; i++) {
        Solve_main();
    }

    cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
    return 0;
}


#include<bits/stdc++.h> // 68 79 "Baklol"
using namespace std;

#define rep(x, k, n)    for(int x = (k); x < (n); ++x)
#define repr(x, k, n)   for(int x = (k); x > (n); --x)
#define tr(it, a)       for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll              long long
#define siz(x)          ((int)(x).size())
#define print(x)        cout << (x) << '\n'
#define prints(x)       cout << (x) << " "
#define eb              emplace_back
#define mp              make_pair
#define F               first
#define S               second
#define o2(x)           ( (x) * (x) )
#define all(x)          (x).begin(), (x).end()
#define allr(x)         (x).rbegin(), (x).rend()
#define clr(x, k)       memset(x, k, sizeof(x))
#define sortall(x)      sort(all(x))
#define sortalld(x)     sort(all(x), greater<int>())
#define printv(v)       rep(x, 0, siz(v)){ prints(v[x]); } cout << '\n'
#define print2dv(V)     rep(y, 0, siz(V)){ printv(V[y]); }
#define out(x)          cout << ((x) ? "Yes" : "No") << '\n';
#define inf             INT_MAX
#define ninf            INT_MIN
#define int             long long // take care of it when too much computation required

typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<bool>    vb;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
typedef vector<vb>      vvb;

template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
    cout << name << " = " << arg1  << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " = " << arg1 <<",";
    debug_out(comma+1, args...);
}

#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const int mod = 1e9+7; // 998244353
const int N = 3e5, M = N;

/*
-> Do not forget to check the edge cases.
-> Do something instead of nothing.
-> Make sure you are submitting the right file.
-> Do not make "easy->complicated", xd... :|.
*/

//-----------------------------------------------------------------------------

int check(int diff, vi temp){
    vi v = temp;
    int res = inf, cnt = 0;
    int n = siz(v);
    rep(i, 1, n){
        int d = v[i] - v[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]--;
                    v[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]--;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]++;
                    v[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]++;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    cnt = 0;
    repr(i, n-1, 0){
        int d = temp[i] - temp[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]--;
                    temp[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]++;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]++;
                    temp[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]--;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    return res;
}

void redundent(){
    int num = 1e9;
    while(num--){
        deb(num);
    }
}

void Solve_main() {
    redundent();
    int n;
    cin >> n;
    vi v(n);
    rep(i, 0, n){
        cin >> v[i];
    }
    if(n == 1){
        cout << 0 << '\n';
        return;
    }
    if(v.back() < v.front()){
        reverse(all(v));
    }
    int diffsum = 0;
    rep(i, 1, n){
        diffsum += v[i] - v[i-1];
    }

    int diff1 = diffsum / (n - 1);
    int diff2 = (diffsum + n - 2) / (n - 1);
    int res = check(diff2, v);
    res = min(res, check(diff1, v));
    if(res >= inf){
        cout << -1 << '\n';
    }
    else{
        cout << res << '\n';
    }
}

//-----------------------------------------------------------------------------

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    // cout << setprecision(12) << fixed;
    int tc = 1;
    // cin >> tc;
    for(int i = 0; i < tc; i++) {
        Solve_main();
    }

    cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
    return 0;
}


#include<bits/stdc++.h> // 68 79 "Baklol"
using namespace std;

#define rep(x, k, n)    for(int x = (k); x < (n); ++x)
#define repr(x, k, n)   for(int x = (k); x > (n); --x)
#define tr(it, a)       for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll              long long
#define siz(x)          ((int)(x).size())
#define print(x)        cout << (x) << '\n'
#define prints(x)       cout << (x) << " "
#define eb              emplace_back
#define mp              make_pair
#define F               first
#define S               second
#define o2(x)           ( (x) * (x) )
#define all(x)          (x).begin(), (x).end()
#define allr(x)         (x).rbegin(), (x).rend()
#define clr(x, k)       memset(x, k, sizeof(x))
#define sortall(x)      sort(all(x))
#define sortalld(x)     sort(all(x), greater<int>())
#define printv(v)       rep(x, 0, siz(v)){ prints(v[x]); } cout << '\n'
#define print2dv(V)     rep(y, 0, siz(V)){ printv(V[y]); }
#define out(x)          cout << ((x) ? "Yes" : "No") << '\n';
#define inf             INT_MAX
#define ninf            INT_MIN
#define int             long long // take care of it when too much computation required

typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<bool>    vb;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
typedef vector<vb>      vvb;

template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
    cout << name << " = " << arg1  << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " = " << arg1 <<",";
    debug_out(comma+1, args...);
}

#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const int mod = 1e9+7; // 998244353
const int N = 3e5, M = N;

/*
-> Do not forget to check the edge cases.
-> Do something instead of nothing.
-> Make sure you are submitting the right file.
-> Do not make "easy->complicated", xd... :|.
*/

//-----------------------------------------------------------------------------

int check(int diff, vi temp){
    vi v = temp;
    int res = inf, cnt = 0;
    int n = siz(v);
    rep(i, 1, n){
        int d = v[i] - v[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]--;
                    v[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]--;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]++;
                    v[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]++;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    cnt = 0;
    repr(i, n-1, 0){
        int d = temp[i] - temp[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]--;
                    temp[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]++;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]++;
                    temp[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]--;
                cnt++;
            }
        }
    }
    // deb(res, cnt);
    res = min(res, cnt);
    // deb(diff, res);
    return res;
}

void Solve_main() {

    int n;
    cin >> n;
    vi v(n);
    rep(i, 0, n){
        cin >> v[i];
    }
    if(n == 1){
        cout << 0 << '\n';
        return;
    }
    if(v.back() < v.front()){
        reverse(all(v));
    }
    // printv(v);
    int diffsum = 0;
    rep(i, 1, n){
        diffsum += v[i] - v[i-1];
        // deb(v[i] - v[i-1]);
    }
    // deb(diffsum);
    int diff1 = diffsum / (n - 1);
    int diff2 = (diffsum + n - 2) / (n - 1);
    // deb(diff1, diff2);
    int res = check(diff2, v);
    // res = min(res, check(diff1, v));
    if(res >= inf){
        cout << -1 << '\n';
    }
    else{
        cout << res << '\n';
    }

}

//-----------------------------------------------------------------------------

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    // cout << setprecision(12) << fixed;
    int tc = 1;
    // cin >> tc;
    for(int i = 0; i < tc; i++) {
        Solve_main();
    }

    cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
    return 0;
}


#include<bits/stdc++.h> // 68 79 "Baklol"
using namespace std;

#define rep(x, k, n)    for(int x = (k); x < (n); ++x)
#define repr(x, k, n)   for(int x = (k); x > (n); --x)
#define tr(it, a)       for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll              long long
#define siz(x)          ((int)(x).size())
#define print(x)        cout << (x) << '\n'
#define prints(x)       cout << (x) << " "
#define eb              emplace_back
#define mp              make_pair
#define F               first
#define S               second
#define o2(x)           ( (x) * (x) )
#define all(x)          (x).begin(), (x).end()
#define allr(x)         (x).rbegin(), (x).rend()
#define clr(x, k)       memset(x, k, sizeof(x))
#define sortall(x)      sort(all(x))
#define sortalld(x)     sort(all(x), greater<int>())
#define printv(v)       rep(x, 0, siz(v)){ prints(v[x]); } cout << '\n'
#define print2dv(V)     rep(y, 0, siz(V)){ printv(V[y]); }
#define out(x)          cout << ((x) ? "Yes" : "No") << '\n';
#define inf             INT_MAX
#define ninf            INT_MIN
#define int             long long // take care of it when too much computation required

typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<bool>    vb;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
typedef vector<vb>      vvb;

template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
    cout << name << " = " << arg1  << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " = " << arg1 <<",";
    debug_out(comma+1, args...);
}

#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const int mod = 1e9+7; // 998244353
const int N = 3e5, M = N;

/*
-> Do not forget to check the edge cases.
-> Do something instead of nothing.
-> Make sure you are submitting the right file.
-> Do not make "easy->complicated", xd... :|.
*/

//-----------------------------------------------------------------------------

int check(int diff, vi temp){
    vi v = temp;
    int res = inf, cnt = 0;
    int n = siz(v);
    rep(i, 1, n){
        int d = v[i] - v[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]--;
                    v[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]--;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == 1){
                    v[i]++;
                    v[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                v[i]++;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    cnt = 0;
    repr(i, n-1, 0){
        int d = temp[i] - temp[i-1];
        if(d > diff){
            d -= diff;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]--;
                    temp[i-1]++;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]++;
                cnt++;
            }
        }
        else if(d < diff){
            d = diff - d;
            if(d >= 2){
                if(d == 2 && i == n-1){
                    temp[i]++;
                    temp[i-1]--;
                    cnt += 2;
                }
                else{
                    cnt = inf;
                    break;
                }
            }
            else if(d == 1){
                temp[i-1]--;
                cnt++;
            }
        }
    }
    res = min(res, cnt);
    return res;
}

void redundent(){
    int num = 1e14;
    int res = 1;
    while(num--){
        res += res;
        deb(num);
    }
}

void Solve_main() {
    redundent();
    int n;
    cin >> n;
    vi v(n);
    rep(i, 0, n){
        cin >> v[i];
    }
    if(n == 1){
        cout << 0 << '\n';
        return;
    }
    if(v.back() < v.front()){
        reverse(all(v));
    }
    int diffsum = 0;
    rep(i, 1, n){
        diffsum += v[i] - v[i-1];
    }

    int diff1 = diffsum / (n - 1);
    int diff2 = (diffsum + n - 2) / (n - 1);
    int res = check(diff2, v);
    res = min(res, check(diff1, v));
    if(res >= inf){
        cout << -1 << '\n';
    }
    else{
        cout << res << '\n';
    }
}

//-----------------------------------------------------------------------------

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    // cout << setprecision(12) << fixed;
    int tc = 1;
    // cin >> tc;
    for(int i = 0; i < tc; i++) {
        Solve_main();
    }

    cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
    return 0;
}


#include <iostream>

using namespace std;

const int N = 100000;
int n, sequance[N];

int calc (int i, int diff, int modi, int preModi) {
    if (i != 0 && i != 1) {
        if (diff != sequance[i-1]+preModi - (sequance[i]+modi))
            return 1e9;
    }else if (i == 1)
        diff = sequance[i-1]+preModi - (sequance[i]+modi);

    if (i == n-1) {
        return 0;
    }

    int keep = calc(i + 1, diff, 0, modi);
    int changePos = 1 + calc(i + 1, diff, 1, modi);
    int changeNeg = 1 + calc(i + 1, diff, -1, modi);

    return min(min(keep, changePos), changeNeg);
}

int main() {

    cin >> n;

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

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        sequance[i] = x;
    }

    int keep = calc(0, 0, 0, 0);
    int changePos = 1 + calc(0, 0, 1, 0);
    int changeNeg = 1 + calc(0, 0, -1, 0);

    int res = min(min(keep, changePos), changeNeg);

    if (res > n)
        res = -1;

    cout << res << endl;

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n;

int alter(int changes, vector<int> ai, int i) {
    //cout << ai[1] << endl;
    if (i == n-1) {
        int diff;

        diff = ai[1] - ai[0];
        for (int i = 1; i < n-1; i++) {
            int newDiff = ai[i+1] - ai[i];

            if (newDiff != diff)
                return -1;
        }

        return changes;
    }

    int cases[3];
    cases[0] = alter(changes, ai, i+1);
    ai[i]++;
    cases[1] = alter(changes+1, ai, i+1);
    ai[i] -= 2;
    cases[2] = alter(changes+1, ai, i+1);

    int min = -1;
    for (int i = 0; i < 3; i++)
        if ((min == -1 && cases[i] != -1) || (cases[i] < min && cases[i] != -1))
            min = cases[i];

    return min;
}

int main() {

    cin >> n;

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

    vector<int> ai;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        ai.push_back(x);
    }

    cout << alter(0, ai, 0) << endl;

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

const int N = 100000;
int n, sequance[N];

int calc (int i, int diff, int modi, int preModi) {
    if (i != 0 && i != 1) {
        if (diff != sequance[i-1]+preModi - (sequance[i]+modi))
            return 1e9;
    }else if (i == 1)
        diff = sequance[i-1]+preModi - (sequance[i]+modi);

    if (i == n-1) {
        return 0;
    }

    int keep = calc(i + 1, diff, 0, modi);
    int changePos = 1 + calc(i + 1, diff, 1, modi);
    int changeNeg = 1 + calc(i + 1, diff, -1, modi);

    return min({keep, changePos, changeNeg});
}

int main() {

    cin >> n;

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

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        sequance[i] = x;
    }

    int keep = calc(0, 0, 0, 0);
    int changePos = 1 + calc(0, 0, 1, 0);
    int changeNeg = 1 + calc(0, 0, -1, 0);

    int res = min({keep, changePos, changeNeg});

    if (res > n)
        res = -1;

    cout << res << endl;

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

const int N = 100000;
int n, sequance[N];

int record[N][N];

int calc (int i, int diff, int modi, int preModi) {
    if (i != 0 && i != 1) {
        if (diff != sequance[i-1]+preModi - (sequance[i]+modi))
            return 1e9;
    }else if (i == 1)
        diff = sequance[i-1]+preModi - (sequance[i]+modi);

    if (i == n-1) {
        return 0;
    }

    int keep = calc(i + 1, diff, 0, modi);
    int changePos = 1 + calc(i + 1, diff, 1, modi);
    int changeNeg = 1 + calc(i + 1, diff, -1, modi);

    return min(min(keep, changePos), changeNeg);
}

int main() {

    cin >> n;

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

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        sequance[i] = x;
    }

    int keep = calc(0, 0, 0, 0);
    int changePos = 1 + calc(0, 0, 1, 0);
    int changeNeg = 1 + calc(0, 0, -1, 0);

    int res = min(min(keep, changePos), changeNeg);

    if (res > n)
        res = -1;

    cout << res << endl;

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n;

int alter(int changes, int diff, vector<int> ai, int i) {
    //cout << ai[1] << endl;
    if (i == 1) {
        int cases[3];

        cases[0] = alter(changes, ai[0]-ai[1], ai, i+1);
        ai[1]++;
        cases[1] = alter(changes+1, ai[0]-ai[1], ai, i+1);
        ai[1] -= 2;
        cases[2] = alter(changes+1, ai[0]-ai[1], ai, i+1);

        int min = -1;
        for (int i = 0; i < 3; i++)
            if ((min == -1 && cases[i] != -1) || (cases[i] < min && cases[i] != -1))
                min = cases[i];

        //cout << min << 'h' << endl;
        return min;
    }

    if (i == n-1) {
        return diff == ai[i-1] - ai[i] ? changes : -1;
    }

    //cout << diff << " " << ai[i-1] - ai[i] << endl;

    if (diff == ai[i-1] - ai[i]) {
        return alter(changes, diff, ai, i+1);

    }else if (diff == ai[i-1] - ai[i]+1) {
        ai[i]++;
        return alter(changes+1, diff, ai, i+1);

    }else if (diff == ai[i-1] - ai[i]-1) {
        ai[i]--;
        return alter(changes+1, diff, ai, i+1);
    }

    return -1;
}

int main() {

    cin >> n;

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

    vector<int> ai;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        ai.push_back(x);
    }

    //cout << alter(0, ai, 0) << endl;
    int cases[3];

    cases[0] = alter(0, 0, ai, 1);
    ai[0]++;
    cases[1] = alter(1, 0, ai, 1);
    ai[0] -= 2;
    cases[2] = alter(1, 0, ai, 1);

    int min = -1;
    for (int i = 0; i < 3; i++)
        if ((min == -1 && cases[i] != -1) || (cases[i] < min && cases[i] != -1))
            min = cases[i];

    cout << min << endl;

    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n;

int alter(int changes, int diff, vector<int> ai, int i) {
    if (i == 1) {
        int cases[3];

        cases[0] = alter(changes, ai[0]-ai[1], ai, i+1);
        ai[1]++;
        cases[1] = alter(changes+1, ai[0]-ai[1], ai, i+1);
        ai[1] -= 2;
        cases[2] = alter(changes+1, ai[0]-ai[1], ai, i+1);

        int min = -1;
        for (int i = 0; i < 3; i++)
            if ((min == -1 && cases[i] != -1) || (cases[i] < min && cases[i] != -1))
                min = cases[i];

        //cout << min << 'h' << endl;
        return min;
    }

    if (i == n) {
        return diff == ai[i-2] - ai[i-1] ? changes : -1;
    }

    //cout << diff << " " << ai[i-1] - ai[i] << endl;

    if (diff == ai[i-1] - ai[i]) {
        return alter(changes, diff, ai, i+1);

    }else if (diff == ai[i-1] - (ai[i]+1)) {
        ai[i]++;
        return alter(changes+1, diff, ai, i+1);

    }else if (diff == ai[i-1] - (ai[i]-1)) {
        ai[i]--;
        return alter(changes+1, diff, ai, i+1);
    }

    return -1;
}

int main() {

    cin >> n;

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

    vector<int> ai;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        ai.push_back(x);
    }

    //cout << alter(0, ai, 0) << endl;
    int cases[3];

    cases[0] = alter(0, 0, ai, 1);
    ai[0]++;
    cases[1] = alter(1, 0, ai, 1);
    ai[0] -= 2;
    cases[2] = alter(1, 0, ai, 1);

    int min = -1;
    for (int i = 0; i < 3; i++)
        if ((min == -1 && cases[i] != -1) || (cases[i] < min && cases[i] != -1))
            min = cases[i];

    cout << min << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vt vector
#define pb push_back
#define F first
#define S second
#define rep(i,l,r) for(int i = (l); i <= (r); i++)

const int N = 5e5;
int n,a[N],b[N];

void solve() {
    cin >> n;
    rep(i,1,n) cin >> a[i];
    if(n <= 2) {
        cout << 0;
        return;
    }
    ll re = LLONG_MAX;
    rep(f,-1,1) {
        rep(s,-1,1) {
            ll rs = (f != 0) + (s != 0);
            c[2] = a[2] + s;
            c[1] = a[1] + f;
            int x = a[2] - a[1];
            rep(i,3,n) {
                c[i] = c[i - 1] + x;
            }
            rep(i,3,n) {
                if(a[i] != c[i]) {
                    if(abs(a[i] - c[i]) > 1) {
                        rs = LLONG_MAX;
                        break;
                    } else rs++;
                }
            }
            re = min(re,rs);
        }
    }
    cout << re;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vt vector
#define pb push_back
#define F first
#define S second
#define rep(i,l,r) for(int i = (l); i <= (r); i++)

const int N = 5e5;
int n,a[N],c[N];

void solve() {
    cin >> n;
    rep(i,1,n) cin >> a[i];
    if(n <= 2) {
        cout << 0;
        return;
    }
    ll re = LLONG_MAX;
    rep(f,-1,1) {
        rep(s,-1,1) {
            ll rs = (f != 0) + (s != 0);
            c[2] = a[2] + s;
            c[1] = a[1] + f;
            int x = c[2] - c[1];
            rep(i,3,n) {
                c[i] = c[i - 1] + x;
            }
            rep(i,3,n) {
                if(a[i] != c[i]) {
                    if(abs(a[i] - c[i]) > 1) {
                        rs = LLONG_MAX;
                        break;
                    } else rs++;
                }
            }
            re = min(re,rs);
        }
    }
    cout << (re >= LLONG_MAX ? -1 : re);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vt vector
#define pb push_back
#define F first
#define S second
#define rep(i,l,r) for(int i = (l); i <= (r); i++)

const int N = 5e5;
int n,a[N];

void solve() {
    cin >> n;
    rep(i,1,n) cin >> a[i];
    if(n <= 2) {
        cout << 0;
        return;
    }
    ll re = LLONG_MAX;
    rep(f,-1,1) {
        rep(s,-1,1) {
            ll rs = (f != 0) + (s != 0);
            a[2] += s;
            int x = a[2] - a[1];
            rep(i,3,n) {
                if(a[i] - a[i - 1] == x) continue;
                ll y = abs(a[i - 1] + x - a[i]);
                if(y < 2)rs += y;
                else {
                    rs = LLONG_MAX;
                    break;
                }
            }
            re = min(re,rs);
            a[2] -= s;
        }
    }
    cout << re;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vt vector
#define pb push_back
#define F first
#define S second
#define rep(i,l,r) for(int i = (l); i <= (r); i++)

const int N = 5e5;
int n,a[N];

void solve() {
    cin >> n;
    rep(i,1,n) cin >> a[i];
    if(n <= 2) {
        cout << 0;
        return;
    }
    ll re = LLONG_MAX;
    rep(f,-1,1) {
        rep(s,-1,1) {
            ll rs = (f != 0) + (s != 0);
            a[2] += s;
            a[1] += f;
            int x = a[2] - a[1];
            rep(i,3,n) {
                if(a[i] - a[i - 1] == x) continue;
                ll y = abs(a[i - 1] + x - a[i]);
                if(y < 2)rs += y;
                else {
                    rs = LLONG_MAX;
                    break;
                }
            }
            re = min(re,rs);
            a[2] -= s;
        }
    }
    cout << re;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vt vector
#define pb push_back
#define F first
#define S second
#define rep(i,l,r) for(int i = (l); i <= (r); i++)

const int N = 5e5;
int n,a[N],c[N];

void solve() {
    cin >> n;
    rep(i,1,n) cin >> a[i];
    if(n <= 2) {
        cout << 0;
        return;
    }
    ll re = LLONG_MAX;
    rep(f,-1,1) {
        rep(s,-1,1) {
            ll rs = (f != 0) + (s != 0);
            c[2] = a[2] + s;
            c[1] = a[1] + f;
            int x = c[2] - c[1];
            rep(i,3,n) {
                c[i] = c[i - 1] + x;
            }
            rep(i,3,n) {
                if(a[i] != c[i]) {
                    if(abs(a[i] - c[i]) > 1) {
                        rs = LLONG_MAX;
                        break;
                    } else rs++;
                }
            }
            re = min(re,rs);
        }
    }
    cout << re;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vt vector
#define pb push_back
#define F first
#define S second
#define rep(i,l,r) for(int i = (l); i <= (r); i++)

const int N = 5e5;
int n,a[N];

void solve() {
    cin >> n;
    rep(i,1,n) cin >> a[i];
    if(n <= 2) {
        cout << 0;
        return;
    }
    ll re = LLONG_MAX;
    rep(f,-1,1) {
        rep(s,-1,1) {
            ll rs = (f != 0) + (s != 0);
            a[2] += s;
            a[1] += f;
            int x = a[2] - a[1];
            rep(i,3,n) {
                if(a[i] - a[i - 1] == x) continue;
                ll y = abs(a[i - 1] + x - a[i]);
                if(y < 2)rs += y;
                else {
                    rs = LLONG_MAX;
                    break;
                }
            }
            re = min(re,rs);
            a[2] -= s;
            a[1] -= f;
        }
    }
    cout << re;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vt vector
#define pb push_back
#define F first
#define S second
#define rep(i,l,r) for(int i = (l); i <= (r); i++)

const int N = 5e5;
int n,a[N],c[N];

void solve() {
    cin >> n;
    rep(i,1,n) cin >> a[i];
    if(n <= 2) {
        cout << 0;
        return;
    }
    ll re = LLONG_MAX;
    rep(f,-1,1) {
        rep(s,-1,1) {
            ll rs = (f != 0) + (s != 0);
            c[2] = a[2] + s;
            c[1] = a[1] + f;
            int x = a[2] - a[1];
            rep(i,3,n) {
                c[i] = c[i - 1] + x;
            }
            rep(i,3,n) {
                if(a[i] != c[i]) {
                    if(abs(a[i] - c[i]) > 1) {
                        rs = LLONG_MAX;
                        break;
                    } else rs++;
                }
            }
            re = min(re,rs);
        }
    }
    cout << re;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}
}


#include <bits/stdc++.h>

using namespace std;

/** Begin fast allocation */
const int MAX_MEM = 1e8;
int mpos = 0;
char mem[MAX_MEM];
inline void * operator new ( size_t n ) {
	assert((mpos += n) <= MAX_MEM);
	return (void *)(mem + mpos - n);
}
inline void operator delete ( void * ) noexcept { }
/** End fast allocation */

#define ull unsigned long long
#define ll long long

#define forn(i, n) for (int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second

const int N = 1e6+47;
ll A[N], B[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> A[i];
		B[i] = A[i];
	}
	if (n == 1 || n == 2) {
		cout << 0 << "\n";
		return 0;
	}
	ll res = N;
	int r = A[1] - A[0];
	for (int i = r-2; i <= r+2; i++) {
		ll ans = 0;
		for (int j = 0; j  < n-1; j++) {
			if (B[j+1] - B[j] > i) {
				B[j]++;
				ans++;
			}
			else if (B[j+1]-B[j] < i) {
				B[j]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		//cout << i << "\n";
		if (check) res = min(ans, res);
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	for (int i = r-2; i <= r+2; i++) {
		ll ans = 0;
		for (int j = 1; j  < n-1; j++) {
			if (B[j+1] - B[j] > i) {
				B[j]++;
				ans++;
			}
			else if (B[j+1]-B[j] < i) {
				B[j]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		if (B[1]-B[0] > i) {
			B[0]++;
			ans++;
		}
		else if (B[1] - B[0] < i){
			B[0]--;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		if (check) res = min(ans, res);
		//cout << i << '\n';
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	if (res != N)  cout << res << "\n";
	else cout << "-1\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/** Begin fast allocation */
const int MAX_MEM = 1e8;
int mpos = 0;
char mem[MAX_MEM];
inline void * operator new ( size_t n ) {
	assert((mpos += n) <= MAX_MEM);
	return (void *)(mem + mpos - n);
}
inline void operator delete ( void * ) noexcept { }
/** End fast allocation */

#define ull unsigned long long
#define ll long long

#define forn(i, n) for (int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second

const int N = 1e6+47;
ll A[N], B[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> A[i];
		B[i] = A[i];
	}
	if (n == 1 || n == 2) {
		cout << 0 << "\n";
		return 0;
	}
	ll res = N;
	int r = A[1] - A[0];
	for (int i = r-2; i <= r+2; i++) {
		ll ans = 0;
		for (int j = 1; j  <= n-1; j++) {
			if (B[j] - B[j-1] > i) {
				B[j]--;
				ans++;
			}
			else if (B[j]-B[j-1] < i) {
				B[j]++;
				ans++;
			}
		}
		/*if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}*/
		if (B[1]-B[0] > i) {
			B[0]++;
			ans++;
		}
		else if (B[1] - B[0] < i){
			B[0]--;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		//cout << i << "\n";
		if (check) res = min(ans, res);
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	for (int i = r-2; i <= r+2; i++) {
		ll ans = 0;
		for (int j = 1; j  < n-1; j++) {
			if (B[j+1] - B[j] > i) {
				B[j]++;
				ans++;
			}
			else if (B[j+1]-B[j] < i) {
				B[j]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		if (B[1]-B[0] > i) {
			B[0]++;
			ans++;
		}
		else if (B[1] - B[0] < i){
			B[0]--;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		if (check) res = min(ans, res);
		//cout << i << '\n';
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	if (res != N)  cout << res << "\n";
	else cout << "-1\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/** Begin fast allocation */
const int MAX_MEM = 1e8;
int mpos = 0;
char mem[MAX_MEM];
inline void * operator new ( size_t n ) {
	assert((mpos += n) <= MAX_MEM);
	return (void *)(mem + mpos - n);
}
inline void operator delete ( void * ) noexcept { }
/** End fast allocation */

#define ull unsigned long long
#define ll long long

#define forn(i, n) for (int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second

const int N = 1e6+47;
ll A[N], B[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> A[i];
		B[i] = A[i];
	}
	if (n == 1 || n == 2) {
		cout << 0 << "\n";
		return 0;
	}
	ll res = N;
	int r = A[1] - A[0];
	for (int i = r-2; i <= r+2; i++) {
		ll ans = 0;
		for (int j = 1; j  <= n-1; j++) {
			if (B[j] - B[j-1] > i) {
				B[j]--;
				ans++;
			}
			else if (B[j]-B[j-1] < i) {
				B[j]++;
				ans++;
			}
		}
		/*if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}*/
		if (B[1]-B[0] > i) {
			B[0]++;
			ans++;
		}
		else if (B[1] - B[0] < i){
			B[0]--;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		//cout << i << "\n";
		if (check) res = min(ans, res);
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	for (int i = r-2; i <= r+2; i++) {
		ll ans = 0;
		for (int j = n-1; j  >= 1; j--) {
			if (B[j] - B[j-1] > i) {
				B[j-1]++;
				ans++;
			}
			else if (B[j]-B[j-1] < i) {
				B[j-1]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		if (check) res = min(ans, res);
		//cout << i << '\n';
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	if (res != N)  cout << res << "\n";
	else cout << "-1\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/** Begin fast allocation */
const int MAX_MEM = 1e8;
int mpos = 0;
char mem[MAX_MEM];
inline void * operator new ( size_t n ) {
	assert((mpos += n) <= MAX_MEM);
	return (void *)(mem + mpos - n);
}
inline void operator delete ( void * ) noexcept { }
/** End fast allocation */

#define ull unsigned long long
#define ll long long

#define forn(i, n) for (int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second

const int N = 1e5+47;
int A[N], B[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> A[i];
		B[i] = A[i];
	}
	if (n == 1 || n == 2) {
		cout << 0 << "\n";
		return 0;
	}
	int res = N;
	int r = A[1] - A[0];
	for (int i = r-2; i <= r+2; i++) {
		int ans = 0;
		for (int j = 0; j  < n-1; j++) {
			if (B[j+1] - B[j] > i) {
				B[j]++;
				ans++;
			}
			else if (B[j+1]-B[j] < i) {
				B[j]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		if (check) res = min(ans, res);
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	if (res != N)  cout << res << "\n";
	else cout << "-1\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/** Begin fast allocation */
const int MAX_MEM = 1e8;
int mpos = 0;
char mem[MAX_MEM];
inline void * operator new ( size_t n ) {
	assert((mpos += n) <= MAX_MEM);
	return (void *)(mem + mpos - n);
}
inline void operator delete ( void * ) noexcept { }
/** End fast allocation */

#define ull unsigned long long
#define ll long long

#define forn(i, n) for (int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second

const int N = 1e5+47;
int A[N], B[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> A[i];
		B[i] = A[i];
	}
	if (n == 1 || n == 2) {
		cout << 0 << "\n";
		return 0;
	}
	int res = N;
	int r = A[1] - A[0];
	for (int i = r-2; i <= r+2; i++) {
		int ans = 0;
		for (int j = 0; j  < n-1; j++) {
			if (B[j+1] - B[j] > i) {
				B[j]++;
				ans++;
			}
			else if (B[j+1]-B[j] < i) {
				B[j]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		if (check) res = min(ans, res);
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	for (int i = r-2; i <= r+2; i++) {
		int ans = 0;
		for (int j = 1; j  < n-1; j++) {
			if (B[j+1] - B[j] > i) {
				B[j]++;
				ans++;
			}
			else if (B[j+1]-B[j] < i) {
				B[j]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		if (B[0]-B[1] > i) {
			B[0]++;
			ans++;
		}
		else if (B[0] - B[1] < i){
			B[0]--;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		if (check) res = min(ans, res);
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	if (res != N)  cout << res << "\n";
	else cout << "-1\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/** Begin fast allocation */
const int MAX_MEM = 1e8;
int mpos = 0;
char mem[MAX_MEM];
inline void * operator new ( size_t n ) {
	assert((mpos += n) <= MAX_MEM);
	return (void *)(mem + mpos - n);
}
inline void operator delete ( void * ) noexcept { }
/** End fast allocation */

#define ull unsigned long long
#define ll long long

#define forn(i, n) for (int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second

const int N = 1e5+47;
int A[N], B[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> A[i];
		B[i] = A[i];
	}
	if (n == 1 || n == 2) {
		cout << 0 << "\n";
		return 0;
	}
	int res = N;
	int r = A[1] - A[0];
	for (int i = r-2; i <= r+2; i++) {
		int ans = 0;
		for (int j = 0; j  < n-1; j++) {
			if (B[j+1] - B[j] > i) {
				B[j]++;
				ans++;
			}
			else if (B[j+1]-B[j] < i) {
				B[j]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		//cout << i << "\n";
		if (check) res = min(ans, res);
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	for (int i = r-2; i <= r+2; i++) {
		int ans = 0;
		for (int j = 1; j  < n-1; j++) {
			if (B[j+1] - B[j] > i) {
				B[j]++;
				ans++;
			}
			else if (B[j+1]-B[j] < i) {
				B[j]--;
				ans++;
			}
		}
		if (B[n-1]-B[n-2] > i) {
			B[n-1]--;
			ans++;
		}
		else if (B[n-1] - B[n-2] < i){
			B[n-1]++;
			ans++;
		}
		if (B[1]-B[0] > i) {
			B[0]++;
			ans++;
		}
		else if (B[1] - B[0] < i){
			B[0]--;
			ans++;
		}
		int check = 1;
		for (int j = 1; j < n; j++) {
			if (B[j] - B[j-1] != i){
				check =  0;
				break;
			}
		}
		if (check) res = min(ans, res);
		//cout << i << '\n';
		for (int j = 0; j < n; j++) {
			//cout << B[j] << " ";
			B[j] = A[j];
		}
		//cout << "\n";
	}
	if (res != N)  cout << res << "\n";
	else cout << "-1\n";
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

void solve() {
    ll n;
    cin>>n;
    ll arr[n];
    for(ll i=0; i<n; i++)
        cin>>arr[i];
    // if n less than 2 print 0
    if(n<=2){
        cout<<'0'<<endl;
        return;
    }
    ll a[3] = {0, 1, -1};
    ll first, second, minCount = INT_MAX;
    for(ll i=0; i<3; i++){
        first = arr[0] + a[i];
        for(ll j=0; j<3; j++){
            second = arr[1] + a[j];
            ll d = second - first;
            ll count = 0;
            bool ok = true;
            for(ll k=0; k<n; k++){
                // cout<<(first + i*d)<<" "<<arr[i]<<endl;
                ll y = arr[k] - (first + k*d);
                if((y == 1) || (y == 0) || (y == -1)){
                    if(y != 0){
                        count++;
                    }
                }
                else{
                    ok = false;
                    break;
                }
            }
            if(ok){
                if(minCount > count){
                    minCount = count;
                }
            }
        }
    }
    if(minCount != INT_MAX){
        cout<<minCount<<endl;
        return;
    }
    cout<<"-1"<<endl;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
    freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    // cin>>t;
    t = 1;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

void solve() {
    ll n;
    cin>>n;
    ll arr[n];
    for(ll i=0; i<n; i++)
        cin>>arr[i];
    // if n less than 2 print 0
    if(n<=2){
        cout<<'0'<<endl;
        return;
    }
    // Check whether it is already a arithmetic sequence
    ll t = arr[1] - arr[0];
    bool already = true;
    for(int i=0; i<n; i++){
        if(arr[0] + t*i != arr[i]){
            already = false;
        }
    }
    if(already){
        cout<<"0"<<endl;
        return;
    }
    ll a[3] = {-1, 0, 1};
    ll first, second;
    for(ll i=0; i<3; i++){
        first = arr[0] + a[i];
        for(ll j=0; j<3; j++){
            second = arr[1] + a[j];
            ll d = second - first;
            ll count = 0;
            bool ok = true;
            for(ll k=0; k<n; k++){
                // cout<<(first + i*d)<<" "<<arr[i]<<endl;
                ll y = arr[k] - (first + k*d);
                if((y == 1) || (y == 0) || (y == -1)){
                    if(y != 0){
                        count++;
                    }
                }
                else{
                    ok = false;
                    break;
                }
            }
            if(ok){
                cout<<count<<endl;
                return;
            }
        }
    }
    cout<<"-1"<<endl;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
    freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    // cin>>t;
    t = 1;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

void solve() {
    ll n;
    cin>>n;
    ll arr[n];
    for(int i=0; i<n; i++)
        cin>>arr[i];
    if(n<=2){
        cout<<'0'<<endl;
    }
    ll a[3] = {-1, 0, 1};
    for(int i=0; i<3; i++){
        ll first = arr[0] + a[i];
        ll second = arr[1];
        for(int k=0; k<3; k++){
            second += a[k];
            ll d = second - first;
            // cout<<"diff "<<d<<endl;
            ll count = 0;
            bool ok = true;
            // cout<<first<<" ";
            for(int i=0; i<n; i++){
                // cout<<(first + i*d)<<" "<<arr[i]<<endl;
                ll y = arr[i] - (first + i*d);
                // cout<<y<<endl;
                if((y == 1) || (y == 0) || (y == -1)){
                    if(y != 0){
                        count++;
                    }
                }
                else{
                    // cout<<y<<endl;
                    ok = false;
                    break;
                }
            }
            // cout<<endl;
            if(ok){
                cout<<count<<endl;
                return;
            }
        }
    }
    cout<<"-1"<<endl;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
    freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    // cin>>t;
    t = 1;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

void solve() {
    ll n;
    cin>>n;
    ll arr[n];
    for(int i=0; i<n; i++)
        cin>>arr[i];
    if(n<=2){
        cout<<'0'<<endl;
        return;
    }
    ll a[3] = {-1, 0, 1};
    for(int i=0; i<3; i++){
        ll first = arr[0] + a[i];
        ll second = arr[1];
        for(int k=0; k<3; k++){
            second += a[k];
            ll d = second - first;
            // cout<<"diff "<<d<<endl;
            ll count = 0;
            bool ok = true;
            // cout<<first<<" ";
            for(int i=0; i<n; i++){
                // cout<<(first + i*d)<<" "<<arr[i]<<endl;
                ll y = arr[i] - (first + i*d);
                // cout<<y<<endl;
                if((y == 1) || (y == 0) || (y == -1)){
                    if(y != 0){
                        count++;
                    }
                }
                else{
                    // cout<<y<<endl;
                    ok = false;
                    break;
                }
            }
            // cout<<endl;
            if(ok){
                cout<<count<<endl;
                return;
            }
        }
    }
    cout<<"-1"<<endl;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
    freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    // cin>>t;
    t = 1;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

void solve() {
    ll n;
    cin>>n;
    ll arr[n];
    for(ll i=0; i<n; i++)
        cin>>arr[i];
    // if n less than 2 print 0
    if(n<=2){
        cout<<'0'<<endl;
        return;
    }
    // Check whether it is already a arithmetic sequence
    ll t = arr[1] - arr[0];
    bool already = true;
    for(int i=0; i<n; i++){
        if(arr[0] + t*i != arr[i]){
            already = false;
        }
    }
    if(already){
        cout<<"0"<<endl;
        return;
    }
    ll a[3] = {0, 1, -1};
    ll first, second, minCount = INT_MAX;
    for(ll i=0; i<3; i++){
        first = arr[0] + a[i];
        for(ll j=0; j<3; j++){
            second = arr[1] + a[j];
            ll d = second - first;
            ll count = 0;
            bool ok = true;
            for(ll k=0; k<n; k++){
                // cout<<(first + i*d)<<" "<<arr[i]<<endl;
                ll y = arr[k] - (first + k*d);
                if((y == 1) || (y == 0) || (y == -1)){
                    if(y != 0){
                        count++;
                    }
                }
                else{
                    ok = false;
                    break;
                }
            }
            if(ok){
                if(minCount > count){
                    minCount = count;
                }
            }
        }
    }
    if(minCount != INT_MAX){
        cout<<minCount<<endl;
        return;
    }
    cout<<"-1"<<endl;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
    freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    // cin>>t;
    t = 1;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

void solve() {
    ll n;
    cin>>n;
    ll arr[n];
    for(ll i=0; i<n; i++)
        cin>>arr[i];
    // if n less than 2 print 0
    if(n<=2){
        cout<<'0'<<endl;
        return;
    }
    // Check whether it is already a arithmetic sequence
    ll t = arr[1] - arr[0];
    bool already = true;
    for(int i=0; i<n; i++){
        if(arr[0] + t*i != arr[i]){
            already = false;
        }
    }
    if(already){
        cout<<"0"<<endl;
        return;
    }
    ll a[3] = {0, 1, -1};
    ll first, second;
    for(ll i=0; i<3; i++){
        first = arr[0] + a[i];
        for(ll j=0; j<3; j++){
            second = arr[1] + a[j];
            ll d = second - first;
            ll count = 0;
            bool ok = true;
            for(ll k=0; k<n; k++){
                // cout<<(first + i*d)<<" "<<arr[i]<<endl;
                ll y = arr[k] - (first + k*d);
                if((y == 1) || (y == 0) || (y == -1)){
                    if(y != 0){
                        count++;
                    }
                }
                else{
                    ok = false;
                    break;
                }
            }
            if(ok){
                cout<<count<<endl;
                return;
            }
        }
    }
    cout<<"-1"<<endl;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
    freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    // cin>>t;
    t = 1;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

void solve() {
    ll n;
    cin>>n;
    ll arr[n];
    for(ll i=0; i<n; i++)
        cin>>arr[i];
    if(n<=2){
        cout<<'0'<<endl;
        return;
    }
    ll t = arr[1] - arr[0];
    bool already = true;
    for(int i=0; i<n; i++){
        if(arr[0] + t*i != arr[i]){
            already = false;
        }
    }
    if(already){
        cout<<"0"<<endl;
        return;
    }
    ll a[3] = {-1, 0, 1};
    for(ll i=0; i<3; i++){
        ll first = arr[0] + a[i];
        ll second = arr[1];
        for(ll k=0; k<3; k++){
            second += a[k];
            ll d = second - first;
            // cout<<"diff "<<d<<endl;
            ll count = 0;
            bool ok = true;
            // cout<<first<<" ";
            for(ll i=0; i<n; i++){
                // cout<<(first + i*d)<<" "<<arr[i]<<endl;
                ll y = arr[i] - (first + i*d);
                // cout<<y<<endl;
                if((y == 1) || (y == 0) || (y == -1)){
                    if(y != 0){
                        count++;
                    }
                }
                else{
                    // cout<<y<<endl;
                    ok = false;
                    break;
                }
            }
            // cout<<endl;
            if(ok){
                cout<<count<<endl;
                return;
            }
        }
    }
    cout<<"-1"<<endl;
    return;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
    freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    // cin>>t;
    t = 1;
    while(t--) {
        solve();
    }
    return 0;
}


/*
 Templatka c++ Kacper Fis																							   																											   //
*/

#include <iostream>
#include <cstdio>
#include <limits>
#include <cstdlib>
#include <utility>
#include <iterator>
#include <bitset>
#include <list>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <string>
#include <time.h>
#include <iomanip>
#include <sstream>
#include <bits/stdc++.h>

using namespace std;

const int INF = 1000000001;
const float PI = 3.14;
typedef long long LL;

typedef vector<int> vi;
typedef list<int> li;
typedef queue<int> qi;
typedef pair<int,int> ii;
typedef map<string,int> msi; //np msi['nazwa'] = 'liczba'
typedef vector<ii> vii; //vector par<int>, do tworzenia tablicy dla grafów wagowych #dijkstra #Bellman_Ford
typedef priority_queue< ii, vector<ii>, greater<ii> > pq; //kolejka priorytetowa vectorów par(top()=min) #dijkstra

vector<ii>::iterator iter;

#define print_list(x) for(it = (x).begin(); it != (x).end(); it++) {cout << *it << " ";}
#define print_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++) {cout << *it2 << " ";}
#define search_list(x) for(it = (x).begin(); it != (x).end(); it++)
#define search_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++);
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)

///////////////////////////////////////////////GLOBAL DEFINITIONS////////////////////////////////////////////////////////

int n;
int tab[100005];

///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> tab[i];
	}

	int roznice[100005];
	for(int i = 0; i < n-1; i++){
		roznice[i] = abs(tab[i+1] - tab[i]);
	}

	sort(roznice, roznice + (n-1));
	reverse(roznice, roznice + (n-1));
	int ans = 0;
	//cout << "roznice: " << endl;
	for(int i = 0; i < n-2; i++){
		//cout << roznice[i] << " ";
		ans = max(ans, roznice[i] - roznice[i + 1]);
	}

	if(ans >= 4) cout << "-1" << endl;
	else cout << ans << endl;

	return 0;
}


/*
 Templatka c++ Kacper Fis																							   																											   //
*/

#include <iostream>
#include <cstdio>
#include <limits>
#include <cstdlib>
#include <utility>
#include <iterator>
#include <bitset>
#include <list>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <string>
#include <time.h>
#include <iomanip>
#include <sstream>
#include <bits/stdc++.h>

using namespace std;

const int INF = 1000000001;
const float PI = 3.14;
typedef long long LL;

typedef vector<int> vi;
typedef list<int> li;
typedef queue<int> qi;
typedef pair<int,int> ii;
typedef map<string,int> msi; //np msi['nazwa'] = 'liczba'
typedef vector<ii> vii; //vector par<int>, do tworzenia tablicy dla grafów wagowych #dijkstra #Bellman_Ford
typedef priority_queue< ii, vector<ii>, greater<ii> > pq; //kolejka priorytetowa vectorów par(top()=min) #dijkstra

vector<ii>::iterator iter;

#define print_list(x) for(it = (x).begin(); it != (x).end(); it++) {cout << *it << " ";}
#define print_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++) {cout << *it2 << " ";}
#define search_list(x) for(it = (x).begin(); it != (x).end(); it++)
#define search_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++);
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)

///////////////////////////////////////////////GLOBAL DEFINITIONS////////////////////////////////////////////////////////

int n;
int tab[100005];

///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> tab[i];
	}

	int roznice[100005];
	for(int i = 0; i < n-1; i++){
		roznice[i] = abs(tab[i+1] - tab[i]);
	}

	sort(roznice, roznice + (n-1));
	reverse(roznice, roznice + (n-1));
	int ans = 0;
	//cout << "roznice: " << endl;
	for(int i = 0; i < n-2; i++){
		//cout << roznice[i] << " ";
		ans = max(ans, roznice[i] - roznice[i + 1]);
	}

	if(ans > 4) cout << "-1" << endl;
	else cout << ans << endl;

	return 0;
}


/*
 Templatka c++ Kacper Fis																							   																											   //
*/

#include <iostream>
#include <cstdio>
#include <limits>
#include <cstdlib>
#include <utility>
#include <iterator>
#include <bitset>
#include <list>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <string>
#include <time.h>
#include <iomanip>
#include <sstream>
#include <bits/stdc++.h>

using namespace std;

const int INF = 1000000001;
const float PI = 3.14;
typedef long long LL;

typedef vector<int> vi;
typedef list<int> li;
typedef queue<int> qi;
typedef pair<int,int> ii;
typedef map<string,int> msi; //np msi['nazwa'] = 'liczba'
typedef vector<ii> vii; //vector par<int>, do tworzenia tablicy dla grafów wagowych #dijkstra #Bellman_Ford
typedef priority_queue< ii, vector<ii>, greater<ii> > pq; //kolejka priorytetowa vectorów par(top()=min) #dijkstra

vector<ii>::iterator iter;

#define print_list(x) for(it = (x).begin(); it != (x).end(); it++) {cout << *it << " ";}
#define print_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++) {cout << *it2 << " ";}
#define search_list(x) for(it = (x).begin(); it != (x).end(); it++)
#define search_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++);
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)

///////////////////////////////////////////////GLOBAL DEFINITIONS////////////////////////////////////////////////////////

int n;
int tab[100005];

///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> tab[i];
	}

	int roznice[100005];
	for(int i = 0; i < n-1; i++){
		roznice[i] = abs(tab[i+1] - tab[i]);
	}

	sort(roznice, roznice + (n-1));
	reverse(roznice, roznice + (n-1));
	int ans = 0;
	//cout << "roznice: " << endl;
	for(int i = 0; i < n-2; i++){
		//cout << roznice[i] << " ";
		ans = max(ans, roznice[i] - roznice[i + 1]);
	}

	if(ans > 5) cout << "-1" << endl;
	else cout << ans << endl;

	return 0;
}


/*
 Templatka c++ Kacper Fis																							   																											   //
*/

#include <iostream>
#include <cstdio>
#include <limits>
#include <cstdlib>
#include <utility>
#include <iterator>
#include <bitset>
#include <list>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <string>
#include <time.h>
#include <iomanip>
#include <sstream>
#include <bits/stdc++.h>

using namespace std;

const int INF = 1000000001;
const float PI = 3.14;
typedef long long LL;

typedef vector<int> vi;
typedef list<int> li;
typedef queue<int> qi;
typedef pair<int,int> ii;
typedef map<string,int> msi; //np msi['nazwa'] = 'liczba'
typedef vector<ii> vii; //vector par<int>, do tworzenia tablicy dla grafów wagowych #dijkstra #Bellman_Ford
typedef priority_queue< ii, vector<ii>, greater<ii> > pq; //kolejka priorytetowa vectorów par(top()=min) #dijkstra

vector<ii>::iterator iter;

#define print_list(x) for(it = (x).begin(); it != (x).end(); it++) {cout << *it << " ";}
#define print_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++) {cout << *it2 << " ";}
#define search_list(x) for(it = (x).begin(); it != (x).end(); it++)
#define search_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++);
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)

///////////////////////////////////////////////GLOBAL DEFINITIONS////////////////////////////////////////////////////////

int n;
LL tab[100005];

///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> tab[i];
	}

	LL roznice[100005];
	for(int i = 0; i < n-1; i++){
		roznice[i] = abs(tab[i+1] - tab[i]);
	}

	sort(roznice, roznice + (n-1));
	reverse(roznice, roznice + (n-1));
	LL ans = 0;
	//cout << "roznice: " << endl;
	for(int i = 0; i < n-2; i++){
		//cout << roznice[i] << " ";
		ans = max(ans, roznice[i] - roznice[i + 1]);
	}

	if(ans > 4) cout << "-1" << endl;
	else cout << ans << endl;

	return 0;
}


/*
 Templatka c++ Kacper Fis																							   																											   //
*/

#include <iostream>
#include <cstdio>
#include <limits>
#include <cstdlib>
#include <utility>
#include <iterator>
#include <bitset>
#include <list>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <string>
#include <time.h>
#include <iomanip>
#include <sstream>
#include <bits/stdc++.h>

using namespace std;

const int INF = 1000000001;
const float PI = 3.14;
typedef long long LL;

typedef vector<int> vi;
typedef list<int> li;
typedef queue<int> qi;
typedef pair<int,int> ii;
typedef map<string,int> msi; //np msi['nazwa'] = 'liczba'
typedef vector<ii> vii; //vector par<int>, do tworzenia tablicy dla grafów wagowych #dijkstra #Bellman_Ford
typedef priority_queue< ii, vector<ii>, greater<ii> > pq; //kolejka priorytetowa vectorów par(top()=min) #dijkstra

vector<ii>::iterator iter;

#define print_list(x) for(it = (x).begin(); it != (x).end(); it++) {cout << *it << " ";}
#define print_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++) {cout << *it2 << " ";}
#define search_list(x) for(it = (x).begin(); it != (x).end(); it++)
#define search_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++);
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)

///////////////////////////////////////////////GLOBAL DEFINITIONS////////////////////////////////////////////////////////

int tab[100005];

///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	int n;
	cin >> n;
	if(n <= 2){
		cout << "0" << endl;
	}
	else{
		for(int i = 0; i < n; i++){
			cin >> tab[i];
		}
		int counter = 0;
		int ans = -1;
		for(int i = -1; i <= 1; i++){
			int s = tab[0] + i;
			for(int j = -1; j <= 1; j++){
				int d = tab[1] + j - s;

				int counter = (i != 0) + (j != 0);
				for(int k = 2; k < n; k++){
					int x = s + k * d;
					if(tab[k] == x) continue;
					else if(tab[k] == x + 1 || tab[k] == x - 1) counter++;
					else{
						counter = -1;
						break;
					}
				}

				if(counter >= 0){
					if((ans < 0) || (counter < ans))
						ans = counter;
				}
			}
		}

		cout << ans << endl;
	}

	return 0;
}


#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, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, 0}, {1, -1},{1, 1} };

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;
	}
	long long ans = 2e18;
	for (int i = 0; i < 9; i++) {
		long long 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;
}


#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, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, 0}, {1, -1},{1, 1} };

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;
	}
	long long ans = 2e18;
	for (int i = 0; i < 9; i++) {
		long long 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 == 2e18)
		ans = -1;
	cout << ans;
}


#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;
}


#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), raz(n - 1);
	for (int i = 0; i < n; i++) cin >> 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]++;
		}
		if(ok)
			ans = min(ans, cnt);
	}
	if (ans == 2e9)
		ans = -1;
	cout << ans;
}


#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; };

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;
	}
	long long ans = 2e18;
	for(int i = -1; i <= 1; i++) for(int j = -1; j<=1; j++) {
		long long cnt = 0;
		cnt = abs(i);
		int razn = a[1] + j - a[0] - i;
		bool ok = true;
		for (int g = 1; g < n; g++) {
			int x = abs(a[g] - (a[0] + i + g * razn));
			if (x > 1) {
				ok = false;
				break;
			}
			cnt += x;
		}
		if(ok)
			ans = min(ans, cnt);
	}
	if (ans == 2e18)
		ans = -1;
	cout << ans;
}