Polycarp likes arithmetic progressions. A sequence $[a_1, a_2, \dots, a_n]$ is called an arithmetic progression if for each $i$ ($1 \le i < n$) the value $a_{i+1} - a_i$ is the same. For example, the sequences $[42]$, $[5, 5, 5]$, $[2, 11, 20, 29]$ and $[3, 2, 1, 0]$ are arithmetic progressions, but $[1, 0, 1]$, $[1, 3, 9]$ and $[2, 3, 1]$ are not.

It follows from the definition that any sequence of length one or two is an arithmetic progression.

Polycarp found some sequence of positive integers $[b_1, b_2, \dots, b_n]$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $1$, an element can be increased by $1$, an element can be left unchanged.

Determine a minimum possible number of elements in $b$ which can be changed (by exactly one), so that the sequence $b$ becomes an arithmetic progression, or report that it is impossible.

It is possible that the resulting sequence contains element equals $0$.


-----Input-----

The first line contains a single integer $n$ $(1 \le n \le 100\,000)$ — the number of elements in $b$.

The second line contains a sequence $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 10^{9})$.


-----Output-----

If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position).


-----Examples-----
Input
4
24 21 14 10

Output
3

Input
2
500 500

Output
0

Input
3
14 5 1

Output
-1

Input
5
1 3 6 9 12

Output
1



-----Note-----

In the first example Polycarp should increase the first number on $1$, decrease the second number on $1$, increase the third number on $1$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $[25, 20, 15, 10]$, which is an arithmetic progression.

In the second example Polycarp should not change anything, because his sequence is an arithmetic progression.

In the third example it is impossible to make an arithmetic progression.

In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $[0, 3, 6, 9, 12]$, which is an arithmetic progression.

Difficulty: introductory

Starter code:

Solutions:
n = int(input())
T = input().split(' ')
for i in range(n):
    T[i] = int(T[i])
m = n+1
if n == 1:
    print(0)
else:
    for a in range(-1, 2):
        for b in range(-1, 2):
            c = True
            p = (T[1]+b) - (T[0]+a)
            tot = 0
            if a!=0:
                tot+=1
            if b!=0:
                tot+=1
            el = T[1]+b
            for j in range(2, n):
                if abs((T[j] - el) - p) <= 1:
                    el += p
                    if T[j] != el:
                        tot+=1
                else:
                    c = False
            if c:
                m = min(m, tot)
    if m <= n:
        print(m)
    else:
        print(-1)


import sys
import operator as op


next(sys.stdin)

xs = list(map(int, next(sys.stdin).rstrip().split()))

if len(xs) <= 2:
    print(0)
    return

frontier = []
frontier.append((0, xs[1] - xs[0], xs[1]))
frontier.append((1, xs[1] + 1 - xs[0], xs[1] + 1))
if xs[1] > 0:
    frontier.append((1, xs[1] - 1 - xs[0], xs[1] - 1))
if xs[0] > 0:
    frontier.append((1, xs[1] - xs[0] + 1, xs[1]))
frontier.append((1, xs[1] - xs[0] - 1, xs[1]))
if xs[0] > 0:
    frontier.append((2, xs[1] + 1 - xs[0] + 1, xs[1] + 1))
frontier.append((2, xs[1] + 1 - xs[0] - 1, xs[1] + 1))
if xs[0] > 0 and xs[1] > 0:
    frontier.append((2, xs[1] - 1 - xs[0] + 1, xs[1] - 1))
if xs[1] > 0:
    frontier.append((2, xs[1] - 1 - xs[0] - 1, xs[1] - 1))


for x in xs[2:]:

    new_frontier = []

    for current, diff, last_x in frontier:

        if diff == x - last_x:
            new_frontier.append((current, diff, x))
        elif diff == x + 1 - last_x:
            new_frontier.append((current + 1, diff, x + 1))
        elif x > 0 and (diff == x - 1 - last_x):
            new_frontier.append((current + 1, diff, x - 1))

    frontier = new_frontier
    if not frontier:
        print(-1)
        return

print(min(frontier, key=op.itemgetter(0))[0])


R = lambda: map(int, input().split())

inf = 10**7

n = int(input())
b = list(R())

def f(a):
    res = 0

    d = a[1] - a[0]
    e = a[1]
    for i in range(2, n):
        e += d
        if abs(a[i] - e) == 1:
            res += 1
        elif a[i] == e:
            continue
        else:
            return inf
    return res

if n <= 2:
    print(0)
else:
    res = inf
    for dx in (-1, 0, 1):
        for dy in (-1, 0, 1):
            b[0] += dx; b[1] += dy
            res = min(res, f(b) + abs(dx) + abs(dy))
            b[0] -= dx; b[1] -= dy
    if res == inf: res = -1
    print(res)

def solve(n, a):
	if n <= 2:
		return 0

	d = [v - u for u, v in zip(a, a[1:])]

	max_d = max(d)
	min_d = min(d)
	if max_d - min_d > 4:
		return -1

	min_cnt = -1
	for d in range(min_d, max_d + 1):
		for d0 in range(-1, 2):
			y = a[0] + d0
			valid = True
			cnt = 0 if d0 == 0 else 1
			for x in a[1:]:
				dx = abs(y + d - x)
				if dx > 1:
					valid = False
					break

				cnt += dx
				y += d
			if valid:
				# print(d)
				if cnt < min_cnt or min_cnt < 0:
					min_cnt = cnt
	return min_cnt

def main():
	n = int(input())
	a = [int(_) for _ in input().split()]

	ans = solve(n, a)
	print(ans)

def __starting_point():
	main()

__starting_point()

def cal(x, y):
	d = a[1] + y - a[0] - x
	wk1 = a[1] + y
	wkans = 0
	for k in range(2,n):
		diff = abs((wk1 + d) - a[k])
		if diff == 1:
			wkans += 1
		elif diff > 1:
			return n + 1
		wk1 += d
	return wkans
			

n = int(input())
a = [int(i) for i in input().split()]

if n > 2:
	ans_final = n + 1
	for i in range(-1, 2):
		for j in range(-1, 2):
			ans_final = min(ans_final, cal(i, j) + abs(i) + abs(j))
else:
	ans_final = 0
if ans_final == n + 1:
	print(-1)
else:
	print(ans_final)


n = int(input())
b = list(map(int, input().split()))
ind = False
minimum = 10 ** 10
z = [[0, -1, 1], [0, -1, 1]]
if n < 3:
	print(0)
else:
	for i in range(3):
		for j in range(3):
			count = 0
			cnt1 = b[0] + z[0][i]
			cnt2 = b[1] + z[1][j]
			if cnt1 != b[0]:
				count += 1
			if cnt2 != b[1]:
				count += 1
			d = cnt2 - cnt1
			prev = cnt2
			#print(prev, cnt1, cnt2)
			ind1 = True
			for k in range(2, n):
				new_d = b[k] - prev
				if abs(new_d - d) > 1:
					ind1 = False
					#print(cnt1, cnt2, d,prev, new_d, b[k])
					break
				else:
					if new_d != d:
						q = d + prev
						count += 1
						prev = q
					else:
						prev = b[k]
			if ind1:
				ind = True
				if count < minimum:
					minimum = count
	if ind:
		print(minimum)
	else:
		print(-1)			





n = int(input())
b = [int(i) for i in input().split()]
_b = []

if n == 1:
    print(0)
    return

def check(diff):
    cnt = 0
    for i in range(2, n):
        if b[i] - _b[-1] == diff:
            _b.append(b[i])
        elif b[i] - _b[-1] - 1 == diff:
            _b.append(b[i] - 1)
            cnt += 1
        elif b[i] - _b[-1] + 1 == diff:
            _b.append(b[i] + 1)
            cnt += 1
        else:
            return -1
    return cnt

ans = n + 1
d = [-1, 0, 1]
for d1 in d:
    for d2 in d:
        _b = [b[0] + d1, b[1] + d2]
        cur = check(_b[1] - _b[0])
        if cur != -1:
            ans = min(ans, cur + abs(d1) + abs(d2))
print(ans if ans < n + 1 else -1)

n = int(input())
m = list(map(int, input().split()))
if n == 1:
    print(0)
else:
    sost = [-1, 0, 1]
    mi = int(1e10)
    for elem in sost:
        for elem2 in sost:
            new_m = m.copy()
            cur_mi = (elem != 0) + (elem2 != 0)
            start = elem + m[0]
            end = elem2 + m[-1]
            new_m[0] = start
            new_m[-1] = end
            f = True
            if abs(start - end) % (n - 1) == 0:
                step = (start - end) // (n - 1) 
                for i in range(1, n - 1):
                    if new_m[i - 1] - new_m[i] != step:
                        if abs(new_m[i - 1] - new_m[i] - step) == 1:
                            cur_mi += 1
                            new_m[i] += (new_m[i - 1] - new_m[i] - step)
                        else:
                            f = False
                            break
                if f:
                    mi = min(mi, cur_mi)
    if mi == int(1e10):
        print(-1)
    else:
        print(mi)

n = int(input())
a = list(map(int, input().split()))

if n == 1 or n == 2:
    print(0)
else:
    diff = [a[1] - a[0], a[1] + 1 - a[0], a[1] - 1 - a[0],
            a[1] - a[0] + 1, a[1] + 1 - a[0] + 1, a[1] - 1 - a[0] + 1,
            a[1] - a[0] - 1, a[1] + 1 - a[0] - 1, a[1] - 1 - a[0] - 1]

    flag1 = False
    num = float("inf")
    flag = True
    pre = a[1]
    d = diff[0]
    rec = 0
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    flag = True
    pre = a[1] + 1
    d = diff[1]
    rec = 1
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    flag = True
    pre = a[1] - 1
    d = diff[2]
    rec = 1
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    flag = True
    pre = a[1]
    d = diff[3]
    rec = 1
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    flag = True
    pre = a[1] + 1
    d = diff[4]
    rec = 2
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    flag = True
    pre = a[1] - 1
    d = diff[5]
    rec = 2
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    flag = True
    pre = a[1]
    d = diff[6]
    rec = 1
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    flag = True
    pre = a[1] + 1
    d = diff[7]
    rec = 2
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    flag = True
    pre = a[1] - 1
    d = diff[8]
    rec = 2
    for j in range(2, n):
        if a[j] - pre == d + 1:
            rec += 1
            pre = a[j] - 1
        elif a[j] - pre == d - 1:
            rec += 1
            pre = a[j] + 1
        elif a[j] - pre == d:
            pre = a[j]
        else:
            flag = False
            break

    if flag:
        flag1 = True
        num = min(num, rec)

    if flag1:
        print(num)
    else:
        print(-1)

n=int(input())
b=list(map(int,input().split()))
#print(b)
l=len(b)

answer=False
best=1e9
if l<=2:
    print(0)
else:    
    for start in [b[0]-1,b[0],b[0]+1]:
        for end in [b[-1]-1,b[-1],b[-1]+1]:
            d=end-start

            canbe=True
            if d%(l-1)==0:
                diff=d//(l-1)
                if d!=0:
                    if diff>0:
                        needseq = list(range(start,end+1,diff))
                    else:
                        needseq = list(range(start,end-1,diff))
                else:
                    needseq=[start]*l
#                print(needseq)
                count=0
                for s1,s2 in zip(b,needseq):
                    if abs(s1-s2)<=1:
                        count+=abs(s1-s2)
                    else:
                        canbe=False
                        break
                if canbe:
                    best=min(best,count)
                    answer=canbe
            else:
                answer=answer or False
    if answer and best!=1e9:
        print(best)
    else:
        print(-1)
                





n = int(input())
a = [int(x) for x in input().split()]
var = [0, -1, 1]

result = n+1

if n <= 2:
  print(0)
  return

for x in var:
  for y in var:
    if result == 0:
      continue

    diffrest = (a[0] + x - (a[n-1] + y)) % (n-1)
    diff = -(a[0] + x - (a[n-1] + y)) // (n-1)
    if diffrest != 0:
      continue

    change = 0 if x == 0 else 1
    last = a[0] + x
    i = 1
    while i < n and change <= n:
      next = last + diff
      ab = abs(next - a[i])
      if ab == 1:
        change += 1
      elif ab > 1:
        change = n+1
      last = next
      i += 1
    
    if change < result:
      result = change

print(result if result <= n else -1)


def count(a):
    res = 0
    delta = a[1] - a[0]

    for i in range(2, len(a)):
        e = a[i] - a[i - 1] - delta
        if abs(e) > 1:
            return -1

        if abs(e) == 1:
            a[i] -= e
            res += 1

    return res


n = int(input())
b = list(map(int, input().split()))
result = -1

if len(b) == 1:
    print(0)
    return

for i in range(-1, 2):
    for j in range(-1, 2):
        cb = b[:]
        cb[0] += i
        cb[1] += j

        cur = count(cb)

        if cur == -1:
            continue

        cur += abs(i) + abs(j)
        if result == -1 or (result != -1 and cur < result):
            result = cur

print(result)


import sys

def ff(q, s):
    c = 0
    su = 1
    for i in range(1, len(q)):
        if(q[i] - q[i - 1] - s == 1):
            c += 1
            q[i] = q[i] - 1
        elif(q[i] - q[i - 1] - s == 0):
            pass
        elif(q[i] - q[i - 1] - s == -1):
            c += 1
            q[i] = q[i] + 1
        else:
            su = 0
            break
    if(su == 1):
        return c
    else:
        return -1

_ = input()
b = list(map(int, input().split()))
s = 0
for i in range(len(b) - 1):
    s += b[i + 1] - b[i]

if(len(b) == 1):
    print(0)
    return
qw = s // (len(b) - 1)
gg = []


q = b[:]
gg.append(ff(q, qw))
q = b[:]
gg.append(ff(q, qw + 1))

q = b[:]
q[0] = q[0] + 1
res = ff(q, qw)
if(res != -1):
    gg.append(res + 1)
q = b[:]
q[0] = q[0] + 1
res = ff(q, qw + 1)
if(res != -1):
    gg.append(res + 1)

q = b[:]
q[0] = q[0] - 1
res = ff(q, qw)
if(res != -1):
    gg.append(res + 1)
q = b[:]
q[0] = q[0] - 1
res = ff(q, qw + 1)
if(res != -1):
    gg.append(res + 1)

cc = []
for i in gg:
    if(i != -1):
        cc.append(i)
if(cc == []):
    print(-1)
else:
    print(min(cc))


m = [0, -1, 1]

def solve(b):
    n = len(b)
    if n < 3: return 0
    minans = -1
    for ii in range(3):
        for jj in range(3):
            b1 = b[0]+m[ii]
            bn = b[n-1]+m[jj]
            dn = bn-b1
            if dn%(n-1) == 0:
                d = dn//(n-1)
                currans = 0
                for i in range(n):
                    if b[i] == b1+i*d:
                        pass
                    elif abs(b[i]-(b1+i*d)) == 1:
                        currans += 1
                    else:
                        currans = -1
                        break
                if minans == -1 or currans != -1 and currans < minans:
                    minans = currans
    return minans

n = int(input())
b = [int(s) for s in input().split()]

print(solve(b))

n = int(input())

lst = [int(x) for x in input().split()]

a = []
for x in range(len(lst) - 1):
    a.append(lst[x + 1] - lst[x])

if len(a) == 0:
    print(0)
else:
    mini = min(a)
    maxi = max(a)

    if maxi - mini > 4:
        print(-1)
    else:
        m1 = (maxi + mini) // 2
        m2 = (maxi + mini + 1) // 2
        ar = []
        
        k = 0
        flag = True
        for x in range(1, n):
            if lst[0] + m1 * x != lst[x] and lst[x] - 1 <= lst[0] + m1 * x <= lst[x] + 1:
                k += 1
            else:
                if lst[0] + m1 * x == lst[x]:
                    continue
                flag = False
                break
        if flag:
            ar.append(k)

        k = 0
        flag = True
        for x in range(1, n):
            if lst[0] + m2 * x != lst[x] and lst[x] - 1 <= lst[0] + m2 * x <= lst[x] + 1:
                k += 1
            else:
                if lst[0] + m2 * x == lst[x]:
                    continue
                flag = False
                break
        if flag:
            ar.append(k)

        k = 1
        flag = True
        lst[0] -= 1
        for x in range(1, n):
            if lst[0] + m1 * x != lst[x] and lst[x] - 1 <= lst[0] + m1 * x <= lst[x] + 1:
                k += 1
            else:
                if lst[0] + m1 * x == lst[x]:
                    continue
                flag = False
                break
        if flag:
            ar.append(k)

        k = 1
        flag = True
        for x in range(1, n):
            if lst[0] + m2 * x != lst[x] and lst[x] - 1 <= lst[0] + m2 * x <= lst[x] + 1:
                k += 1
            else:
                if lst[0] + m2 * x == lst[x]:
                    continue
                flag = False
                break
        if flag:
            ar.append(k)

        k = 1
        lst[0] += 2
        flag = True
        for x in range(1, n):
            if lst[0] + m1 * x != lst[x] and lst[x] - 1 <= lst[0] + m1 * x <= lst[x] + 1:
                k += 1
            else:
                if lst[0] + m1 * x == lst[x]:
                    continue
                flag = False
                break
        if flag:
            ar.append(k)

        k = 1
        flag = True
        for x in range(1, n):
            if lst[0] + m2 * x != lst[x] and lst[x] - 1 <= lst[0] + m2 * x <= lst[x] + 1:
                k += 1
            else:
                if lst[0] + m2 * x == lst[x]:
                    continue
                flag = False
                break
        if flag:
            ar.append(k)

        if len(ar) != 0:
            print(min(ar))
        else:
            print(-1)


def main():
    n = int(input())
    a = [int(x) for x in input().split()]

    if len(a) <= 2:
        print(0)
        return

    final_ans = n + 1
    for start, change_start in [(a[0], False), (a[0]-1, True), (a[0] + 1, True)]:
        for end, change_end in [(a[-1], False), (a[-1]-1, True), (a[-1]+1, True)]:
            d, r = divmod(end - start, n-1)
            if r == 0:
                b = start
                ans = 0
                if change_start:
                    ans += 1
                if change_end:
                    ans += 1

                impossible = False
                for i in range(1, n-1):
                    if impossible:
                        break
                    if a[i] - b == d:
                        b += d
                    elif a[i] - b == d + 1 or a[i] - b == d - 1:
                        ans += 1
                        b += d
                    else:
                        impossible = True

                if not impossible:
                    final_ans = min(final_ans, ans)

    if final_ans < n + 1:
        print(final_ans)
    else:
        print(-1)


def __starting_point():
    main()
__starting_point()

def solve(seq):
    if len(seq) <= 2:
        return 0

    startings = []
    for x in (-1, 0, 1):
        for y in (-1, 0, 1):
            start = (seq[1] + y,  # first el
                     (seq[0] + x - seq[1] - y),  # delta
                     int(x != 0) + int(y != 0))  # number of changes
            startings.append(start)

    changes = []
    for start in startings:
        last_el, delta, num_changes = start
        impossible = False

        for x in seq[2:]:
            if last_el - x == delta:
                last_el = x
                continue
            if last_el - (x - 1) == delta:
                last_el = x - 1
                num_changes += 1
                continue
            if last_el - (x + 1) == delta:
                last_el = x + 1
                num_changes += 1
                continue

            impossible = True
            break

        if not impossible:
            changes.append(num_changes)

    return min(changes or [-1])

if '__main__' == __name__:
    input()
    print(solve(list(map(int, input().strip().split()))))



n=int(input())
b=list(map(int,input().split()))
lo=n+1
if n<=2:
    print(0)
    quit()
for i in range(-1,2):
    for j in range(-1,2):
        bol,c=1,0
        if (b[0]+i-b[n-1]-j)%(n-1):
            continue
        d=-(b[0]+i-b[n-1]-j)//(n-1)
        for k in range(1,n-1):
            d0=b[k]-b[0]-i-d*k
            if abs(d0)>1:
                bol=0
                break
            elif d0:
                c+=1
        if bol:
            lo=min(c+abs(i)+abs(j),lo)
if lo>n:
    print(-1)
else:
    print(lo)


n = int(input())
ans = 200000
a = list(map(int, input().split()))

def test(x, y):
    if ((a[n - 1] + y) - (a[0] + x)) % (n - 1):
        return False
    first = a[0] + x
    last = a[n - 1] + y
    d = (last - first) // (n - 1)
    for i in range(1, n - 1):
        if abs((first + i * d) - a[i]) > 1:
            return False
    return True

def find(x, y):
    res = abs(x) + abs(y)
    first = a[0] + x
    last = a[n - 1] + y
    d = (last - first) // (n - 1)
    for i in range(1, n - 1):
        if abs((first + i * d) - a[i]):
            res += 1
    return res

if n <= 2:
    print(0)
else:
    for i in range(-1, 2):
        for j in range(-1, 2):
            if test(i, j):
                ans = min(ans, find(i, j))
    print(-1 if ans == 200000 else ans)

import copy
def right(a):
    d = a[1] - a[0]
    res, f = 0, True
    for i in range(2, len(a)):
        if a[i] - a[i - 1] == d + 1:
            a[i] -= 1
            res += 1
        elif a[i] - a[i - 1] == d - 1:
            a[i] += 1
            res += 1
        elif a[i] - a[i - 1] != d:
            f = False
            break
    if not f:
        res = 10 ** 9
    return res
n = int(input())
bs = list(map(int, input().split()))
if n <= 2:
    print(0)
else:
    f = n + 1
    bs[0] -= 1
    bs[1] -= 1
    f = min(f, right(bs.copy()) + 2)
    bs[1] += 1
    f = min(f, right(bs.copy()) + 1)
    bs[1] += 1
    f = min(f, right(bs.copy()) + 2)
    bs[0] += 1
    bs[1] -= 2
    f = min(f, right(bs.copy()) + 1)
    bs[1] += 1
    f = min(f, right(bs.copy()))
    bs[1] += 1
    f = min(f, right(bs.copy()) + 1)
    bs[0] += 1
    bs[1] -= 2
    f = min(f, right(bs.copy()) + 2)
    bs[1] += 1
    f = min(f, right(bs.copy()) + 1)
    bs[1] += 1
    f = min(f, right(bs.copy()) + 2)
    if f == n + 1:
        f = -1
    print(f)

n = [int(x) for x in input().rstrip().split()][0]
data = [int(x) for x in input().rstrip().split()]

is_found = False
nudges = [0, -1, 1]
last_idx = len(data) - 1

if n <= 2:
    print(0)
    return
if n == 3:
    last_idx += 1

result = -1
for s_nudge in nudges:
    for e_nudge in nudges:
        s_val = data[0] + s_nudge
        e_val = data[len(data) - 1] + e_nudge
        if ((e_val - s_val) % (n-1)) == 0:
            num_change = abs(e_nudge) + abs(s_nudge)
            diff = (e_val - s_val) / (n-1)

            val = s_val + diff
            for idx in range(1, last_idx):
                value = data[idx]
                if abs(value - val) == 1:
                    num_change += 1
                elif abs(value - val) > 1:
                    break

                val += diff
                if idx == last_idx - 1:
                    if result == -1 or result > num_change:
                        result = num_change
print(result)


import itertools

def read_ints():
    return list(map(int, input().strip().split()))

def read_int():
    return read_ints()[0]


def check(nums, step, pre):
    suma = 0
    # print("Start: ", step)
    for x in nums:
        s = pre-x
        # print(pre, x, s)
        if abs(step-s) > 1:
            # print("BAD")
            raise Exception()
        if step == s:
            pre = x
            continue
        suma += 1
        if pre-(x+1) == step:
            pre = x+1
        else:
            pre = x-1
    # print("OK ", suma)
    return suma


def main():
    n = read_int()
    nums = read_ints()
    if n <= 2:
        print(0)
        return

    suma  = 100000000
    for x, y in itertools.product([-1,0,1], [-1,0,1]):
        a = nums[0]+x
        b = nums[1]+y
        diff = a-b
        try:
            # print(x,y)
            suma = min(suma, check(nums[2:], diff, b)+abs(x)+abs(y))
        except Exception as e:
            # print(repr(e))
            pass


    if suma == 100000000:
        print(-1)
    else:
        print(suma)



main()


R = lambda:map(int, input().split())
n = int(input())
A = list(R())
if n == 1 or n == 2:
	print(0)
	quit()
minn = int(1e9 + 1)
for i in range(-1,2):
	for j in range(-1, 2):
		if (A[0] + i - A[-1] - j) % (n - 1):
			continue
		f = A[0] + i
		g = A[-1] + j
		cnt = abs(i) + abs(j)
		d = (g - f) // (n - 1)
		for x in range(1, n - 1):
			f += d
			if abs(f - A[x]) > 1:
				break
			elif abs(f - A[x]) == 1:
				cnt += 1
		else:
			minn = min(minn, cnt)
if minn == int(1e9 + 1):
	print(-1)
else:
	print(minn)

def main():
    n = int(input())
    arr = list(map(int, input().split()))
    if n in {1, 2}:
        print(0)
        return
    x = arr[1] - arr[0]
    s = {x, x - 1, x - 2, x + 1, x + 2}
    for i in range(2, n):
        x = arr[i] - arr[i - 1]
        new_s = {x, x - 1, x - 2, x + 1, x + 2}
        s = s & new_s
        if not len(s):
            print(-1)
            return
    ans = float('inf')
    for step in s:
        for f in [arr[0], arr[0] + 1, arr[0] - 1]:
            new_arr = [f]
            cnt = 0 if f == arr[0] else 1
            for i in range(1, n):
                new_arr.append(new_arr[-1] + step)
                if abs(new_arr[-1] - arr[i]) > 1:
                    cnt = float('inf')
                    break
                if new_arr[-1] != arr[i]:
                    cnt += 1
            ans = min(ans, cnt)
    if ans == float('inf'):
        print(-1)
    else:
        print(ans)

def __starting_point():
    main()

__starting_point()