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.

Source: codeforces | Difficulty: MEDIUM
Tags: Mathematics

Implementation

Complete search


Solutions:
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)


def ap(arr):
	ans = 10 ** 20
	for i in [1, 0, -1]:
		for j in [1, 0, -1]:
			second = arr[1] + j
			first = arr[0] + i
			d = second - first
			cnt = abs(i) + abs(j)
			prev = second
			for c in range(2, n):
				next_ele = prev + d
				if abs(next_ele - arr[c]) == 1:
					cnt += 1
				elif abs(next_ele - arr[c]) > 1:
					break
				prev = next_ele
			else:
				ans = min(cnt, ans)
	return -1 if ans == 10 ** 20 else ans
n = int(input())
if n < 3:
	print(0)
else:
	arr = [int(x) for x in input().split()]
	print(ap(arr))


def sol():
	am = int(input())
	darr = list(map(int, input().split()))
	if am == 1 or am == 2:
		print(0)
		return 0
	sl = darr[2:]
	f = darr[0]
	s = darr[1]

	def c(arr):
		dif = arr[1] - arr[0]
		counter = 0
		last = arr[0]
		for i in range(1, len(arr)):
			if arr[i] - last == dif:
				last = arr[i]
				continue
			if arr[i] - last == dif - 1:
				last = arr[i] + 1
				counter += 1
				continue
			if arr[i] - last == dif + 1:
				last = arr[i] - 1
				counter += 1
				continue
			break
		else:
			return counter
		return 999999
	d = [2 + c([f + -1] + [s + -1] + sl), 1 + c([f + -1] + [s + 0] + sl), 1 + c([f + 0] + [s + -1] + sl), 0 + c([f + 0] + [s + 0] + sl), 2 + c([f + 1] + [s + 1] + sl), 1 + c([f + 1] + [s + 0] + sl), 1 + c([f + 0] + [s + 1] + sl), 2 + c([f + 1] + [s + 1] + sl), 2 + c([f + -1] + [s + 1] + sl), 2 + c([f + 1] + [s + -1] + sl)]
	m = 9999999
	for i in range(10):
		if d[i] <= 100000:
			m = min(m, d[i])
	print(m if m != 9999999 else -1)
sol()


n = int(input())
lis = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
tmp = [-1, 0, 1]
fin = 1000000000000
for i in tmp:
	for j in tmp:
		arr = lis[:]
		arr[0] += i
		arr[1] += j
		dif = arr[1] - arr[0]
		ans = 0
		if i != 0:
			ans = 1
		if j != 0:
			ans += 1
		for k in range(2, n):
			aa = abs(arr[k] - (arr[k - 1] + dif))
			if aa == 1:
				ans += 1
				arr[k] = arr[k - 1] + dif
			if aa > 1:
				ans = 1000000000000
		fin = min(ans, fin)
if fin == 1000000000000:
	print(-1)
else:
	print(fin)


import sys
n = int(input())
s = list(map(int, input().split()))
if n == 1:
	print(0)
	sys.exit()
if n == 2:
	print(0)
	sys.exit()
s1 = s.copy()
m = n + 3
x1 = 2
s = s1.copy()
s[1] += 1
s[0] += 1
dif1 = s[1] - s[0]
t1 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif1) > 1:
		t1 = False
		x1 = n + 100
		break
	elif s[i + 1] - s[i] - dif1 == -1:
		x1 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif1 == 1:
		s[i + 1] -= 1
		x1 += 1
x2 = 2
s = s1.copy()
s[1] += 1
s[0] -= 1
dif2 = s[1] - s[0]
t2 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif2) > 1:
		t2 = False
		x2 = n + 100
		break
	elif s[i + 1] - s[i] - dif2 == -1:
		x2 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif2 == 1:
		s[i + 1] -= 1
		x2 += 1
x3 = 1
s = s1.copy()
s[1] += 1
dif3 = s[1] - s[0]
t3 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif3) > 1:
		t3 = False
		x3 = n + 100
		break
	elif s[i + 1] - s[i] - dif3 == -1:
		x3 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif3 == 1:
		s[i + 1] -= 1
		x3 += 1
x4 = 2
s = s1.copy()
s[1] -= 1
s[0] += 1
dif4 = s[1] - s[0]
t4 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif4) > 1:
		t4 = False
		x4 = n + 100
		break
	elif s[i + 1] - s[i] - dif4 == -1:
		x4 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif4 == 1:
		s[i + 1] -= 1
		x4 += 1
x5 = 2
s = s1.copy()
s[1] -= 1
s[0] -= 1
dif5 = s[1] - s[0]
t5 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif5) > 1:
		t5 = False
		x5 = n + 100
		break
	elif s[i + 1] - s[i] - dif5 == -1:
		x5 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif5 == 1:
		s[i + 1] -= 1
		x5 += 1
x6 = 1
s = s1.copy()
s[1] -= 1
dif6 = s[1] - s[0]
t6 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif6) > 1:
		t6 = False
		x6 = n + 100
		break
	elif s[i + 1] - s[i] - dif6 == -1:
		x6 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif6 == 1:
		s[i + 1] -= 1
		x6 += 1
x7 = 1
s = s1.copy()
s[0] += 1
dif7 = s[1] - s[0]
t7 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif7) > 1:
		t7 = False
		x7 = n + 100
		break
	elif s[i + 1] - s[i] - dif7 == -1:
		x7 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif7 == 1:
		s[i + 1] -= 1
		x7 += 1
x8 = 1
s = s1.copy()
s[0] -= 1
dif8 = s[1] - s[0]
t8 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif8) > 1:
		t8 = False
		x8 = n + 100
		break
	elif s[i + 1] - s[i] - dif8 == -1:
		x8 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif8 == 1:
		s[i + 1] -= 1
		x8 += 1
x9 = 0
s = s1.copy()
dif9 = s[1] - s[0]
t9 = True
for i in range(1, n - 1):
	if abs(s[i + 1] - s[i] - dif9) > 1:
		t9 = False
		x9 = n + 100
		break
	elif s[i + 1] - s[i] - dif9 == -1:
		x9 += 1
		s[i + 1] += 1
	elif s[i + 1] - s[i] - dif9 == 1:
		s[i + 1] -= 1
		x9 += 1
if t1 or t2 or t3 or t4 or t5 or t6 or t7 or t8 or t9:
	print(min(x1, x2, x3, x4, x5, x6, x7, x8, x9))
else:
	print(-1)


n = int(input())
b = [int(i) for i in input().split()]
_b = []
if n == 1:
	print(0)
	exit(0)

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)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
n = l()[0]
A = l()
if n <= 2:
	print(0)
	exit()
X = [A[0] - 1, A[0], A[0] + 1]
Y = [A[1] - 1, A[1], A[1] + 1]
m = n + 10
for i in X:
	for j in Y:
		B = A[:]
		d = j - i
		pos = 1
		cost = 2
		if i == A[0] and j == A[1]:
			cost = 0
		elif i == A[0] and j != A[1] or (i != A[0] and j == A[1]):
			cost = 1
		A[1] = j
		for k in range(2, n):
			if A[k - 1] + d == A[k]:
				continue
			elif abs(A[k - 1] + d - A[k]) == 1:
				cost += 1
				A[k] = A[k - 1] + d
			else:
				pos = 0
				break
		if pos:
			m = min(m, cost)
		A = B[:]
if m == n + 10:
	print(-1)
else:
	print(m)


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)


from sys import stdout, stdin, setrecursionlimit
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from bisect import *
from string import *
from queue import *
from heapq import *
from math import *
from re import *
from os import *

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))

def fast():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in fast().split()]
(z, zz) = (fast, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def output(answer, end='\n'):
	stdout.write(str(answer) + end)
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
n = int(z())
arr = zzz()
if n <= 2:
	exit(print(0))
ans = -1
for i in range(-1, 2):
	for j in range(-1, 2):
		cnt = []
		(x, y) = (arr[0] + i, arr[1] + j)
		for t in range(n):
			cnt.append(t * (y - x) + x)
		good = 1
		cur = 0
		for t in range(n):
			if abs(arr[t] - cnt[t]) > 1:
				good = 0
			cur += abs(arr[t] - cnt[t])
		if good:
			if ans == -1:
				ans = cur
			else:
				ans = min(ans, cur)
print(ans)


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
			ind1 = True
			for k in range(2, n):
				new_d = b[k] - prev
				if abs(new_d - d) > 1:
					ind1 = False
					break
				elif 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())
if n == 1:
	print(0)
	exit()
(*b,) = map(int, input().split())
ans = 100001
for i in [-1, 0, 1]:
	for j in [-1, 0, 1]:
		a = b.copy()
		(a[0], a[1]) = (b[0] + i, b[1] + j)
		d = a[0] - a[1]
		c = abs(i) + abs(j)
		for k in range(2, n):
			if abs(a[k - 1] - d - a[k]) > 1:
				break
			c += abs(a[k - 1] - d - a[k])
			a[k] = a[k - 1] - d
		else:
			ans = min(c, ans)
print([-1, ans][ans < 100001])


n = int(input())
if n == 1:
	ans = 0
else:
	a = list(map(int, input().split()))
	ans = min((sum(([0, 1, float('inf')][min(2, abs(v + (w - v) * i - x))] for (i, x) in enumerate(a))) for v in range(a[0] - 1, a[0] + 2) for w in range(a[1] - 1, a[1] + 2)))
print(ans if ans < float('inf') else -1)


n = int(input())
a = list(map(int, input().split()))
ok = 10 ** 9
if n <= 2:
	print(0)
else:
	for i in range(-1, 2):
		p = a[0] + i
		for j in range(-1, 2):
			k = 2
			q = a[1] + j
			r = q - p
			f = 0
			c = abs(i) + abs(j)
			while k < n:
				m = p + k * r
				if abs(m - a[k]) > 1:
					f = 1
					break
				else:
					c += abs(m - a[k])
				k = k + 1
			if f == 0:
				ok = min(ok, c)
				break
	if ok == 10 ** 9:
		print(-1)
	else:
		print(ok)


from itertools import product

def main():
	n = int(input())
	if n < 3:
		print(0)
		return
	aa = list(map(int, input().split()))
	(a, b, res) = (aa[0], aa[-1], [])
	for (start, stop) in product((a - 1, a, a + 1), (b - 1, b, b + 1)):
		if not (stop - start) % (n - 1):
			(r, step) = (0, (stop - start) // (n - 1))
			for (i, a) in enumerate(aa):
				b = start + step * i
				if a - 2 < b < a + 2:
					r += a != b
				else:
					break
			else:
				res.append(r)
	print(min(res, default=-1))
main()


n = int(input())
l = input().split()
li = [int(i) for i in l]
if n == 1 or n == 2:
	print(0)
	quit()
d = li[1] - li[0]
maxa = 10 ** 18
for start in [li[0] - 1, li[0], li[0] + 1]:
	for diff in [d - 2, d - 1, d, d + 1, d + 2]:
		poss = 1
		count = 0
		for i in range(n):
			z = abs(li[i] - (start + i * diff))
			if z <= 1:
				count += z
			else:
				poss = 0
				break
		if poss:
			maxa = min(maxa, count)
if maxa == 10 ** 18:
	print(-1)
	quit()
print(maxa)


n = int(input())
if n < 3:
	ans = 0
else:
	seq = [int(x) for x in input().split()]
	ans = float('inf')
	for x in range(-1, 2):
		for y in range(-1, 2):
			trial = seq[:]
			trial[0] += x
			trial[1] += y
			(diff, change) = (trial[1] - trial[0], abs(x) + abs(y))
			for i in range(2, n):
				if abs(trial[i] - trial[i - 1] - diff) > 1:
					change = float('inf')
					break
				change += abs(trial[i] - trial[i - 1] - diff)
				trial[i] = trial[i - 1] + diff
			ans = min(ans, change)
	ans = -1 if ans == float('inf') else ans
print(ans)


n = int(input())
a = list(map(int, input().split()))
if n == 1 or n == 2:
	print(0)
else:
	ans = n + 1
	for i in range(a[0] - 1, a[0] + 2):
		for j in range(a[1] - 1, a[1] + 2):
			t = 0
			if i != a[0]:
				t += 1
			if j != a[1]:
				t += 1
			f = False
			d = j - i
			for x in range(2, n):
				if a[x] - 1 <= i + x * d <= a[x] + 1:
					if i + x * d != a[x]:
						t += 1
				else:
					f = True
					break
			if not f:
				ans = min(ans, t)
	if ans == n + 1:
		print(-1)
	else:
		print(ans)


def funk(l, n, count):
	true = 0
	z = l[1] - l[0]
	for i in range(1, n - 1):
		x = l[i + 1] - l[i] - z
		if x >= 2 or x <= -2:
			true = 1
			break
		elif x == 1:
			l[i + 1] -= 1
			count += 1
		elif x == -1:
			l[i + 1] += 1
			count += 1
	if true == 1:
		return 9999999999999
	return count
n = int(input())
l = [int(x) for x in input().split()]
if n == 1 or n == 2:
	print(0)
else:
	l1 = l[:]
	temp = funk(l1, n, 0)
	l[0] += 1
	l1 = l[:]
	temp = min(temp, funk(l1, n, 1))
	l[0] -= 2
	l1 = l[:]
	temp = min(temp, funk(l1, n, 1))
	l[0] += 1
	l[1] += 1
	l1 = l[:]
	temp = min(temp, funk(l1, n, 1))
	l[1] -= 2
	l1 = l[:]
	temp = min(temp, funk(l1, n, 1))
	l[0] += 1
	l[1] += 2
	l1 = l[:]
	temp = min(temp, funk(l1, n, 2))
	l[0] -= 2
	l1 = l[:]
	temp = min(temp, funk(l1, n, 2))
	l[0] += 2
	l[1] -= 2
	l1 = l[:]
	temp = min(temp, funk(l1, n, 2))
	l[0] -= 2
	l1 = l[:]
	temp = min(temp, funk(l1, n, 2))
	if temp == 9999999999999:
		print(-1)
	else:
		print(temp)


n = int(input())
b = list(map(int, input().split()))
if n <= 2:
	print('0')
	exit()
ans = temp = c = f = 0
ans = 100000000
for i in range(-1, 2):
	for j in range(-1, 2):
		c = 0
		a1 = b[0] + i
		a2 = b[1] + j
		d = a2 - a1
		c = 0
		if i != 0:
			temp = 1
		else:
			temp = 0
		for k in range(1, n):
			ai = a1 + k * d
			if abs(ai - b[k]) > 1:
				c = -1
				break
			else:
				temp += abs(ai - b[k])
		if c != -1:
			f = 1
			ans = min(temp, ans)
if f == 0:
	print('-1')
else:
	print(ans)


def get_arithmetic(b, count):
	d = b[1] - b[0]
	flag = True
	for i in range(2, len(b)):
		x = b[i - 1] + d - b[i]
		if x != 0:
			if x == 1 or (x == -1 and b[i] != 0):
				b[i] += x
				count += 1
			else:
				flag = False
				break
	if flag == True:
		return count
	else:
		return -1

def find_arithmetic_progression(a):
	min_count = -1
	b = [a[i] for i in range(0, len(a))]
	count = get_arithmetic(b, 0)
	if count != -1 and (count < min_count or min_count == -1):
		min_count = count
	b = [a[i] for i in range(0, len(a))]
	b[0] += 1
	count = get_arithmetic(b, 1)
	if count != -1 and (count < min_count or min_count == -1):
		min_count = count
	b = [a[i] for i in range(0, len(a))]
	if b[0] != 0:
		b[0] -= 1
		count = get_arithmetic(b, 1)
		if count != -1 and (count < min_count or min_count == -1):
			min_count = count
	b = [a[i] for i in range(0, len(a))]
	b[1] += 1
	count = get_arithmetic(b, 1)
	if count != -1 and (count < min_count or min_count == -1):
		min_count = count
	b = [a[i] for i in range(0, len(a))]
	b[0] += 1
	b[1] += 1
	count = get_arithmetic(b, 2)
	if count != -1 and (count < min_count or min_count == -1):
		min_count = count
	b = [a[i] for i in range(0, len(a))]
	if b[0] != 0:
		b[0] -= 1
		b[1] += 1
		count = get_arithmetic(b, 2)
		if count != -1 and (count < min_count or min_count == -1):
			min_count = count
	b = [a[i] for i in range(0, len(a))]
	if b[1] != 0:
		b[1] -= 1
		count = get_arithmetic(b, 1)
		if count != -1 and (count < min_count or min_count == -1):
			min_count = count
	b = [a[i] for i in range(0, len(a))]
	if b[1] != 0:
		b[0] += 1
		b[1] -= 1
		count = get_arithmetic(b, 2)
		if count != -1 and (count < min_count or min_count == -1):
			min_count = count
	b = [a[i] for i in range(0, len(a))]
	if b[1] != 0 and b[0] != 0:
		b[0] -= 1
		b[1] -= 1
		count = get_arithmetic(b, 2)
		if count != -1 and (count < min_count or min_count == -1):
			min_count = count
	return min_count
n = int(input())
if n <= 2:
	print(0)
else:
	b = input().split(' ')
	b = [int(b[i]) for i in range(0, len(b))]
	print(find_arithmetic_progression(b))


from sys import stdin, stdout

def rint():
	return map(int, stdin.readline().split())
n = int(input())
b = list(rint())
if n == 1:
	print(0)
	exit()
c = []
for i in range(1, n):
	c.append(b[i] - b[i - 1])
min_c = min(c)
max_c = max(c)
if max_c - min_c > 4:
	print(-1)
	exit()
flag = 0
min_cnt = 10 ** 100
for j in range(min_c, max_c + 1):
	flag = 0
	for k in [-1, 0, 1]:
		flag = 0
		cnt = 0
		if k != 0:
			cnt += 1
		tmp = b[0] + k
		for i in range(1, n):
			tmp = tmp + j
			if abs(tmp - b[i]) > 1:
				flag = 1
				break
			cnt += abs(tmp - b[i])
		if flag:
			continue
		min_cnt = min(cnt, min_cnt)
if min_cnt > 10 ** 99:
	print(-1)
else:
	print(min_cnt)


import sys
input = sys.stdin.readline

def sim(n, b, d):
	cur = b[0]
	count = 0
	for i in range(1, n):
		cur += d
		diff = abs(b[i] - cur)
		if diff == 0:
			continue
		elif diff == 1:
			count += 1
		else:
			return float('inf')
	return count

def solve(n, b):
	if n <= 2:
		return 0
	(a1, a2) = (b[0], b[1])
	d = a2 - a1
	candidates = {d - 2, d - 1, d, d + 1, d + 2}
	best = float('inf')
	for d in candidates:
		b[0] = a1
		best = min(best, sim(n, b, d))
		for first in [a1 - 1, a1 + 1]:
			b[0] = first
			best = min(best, sim(n, b, d) + 1)
	if best == float('inf'):
		return -1
	else:
		return best
n = int(input())
b = list(map(int, input().split()))
print(solve(n, b))


n = int(input())
l = list(map(int, input().split()))
if len(l) < 3:
	print(0)
	exit()
mi = int(1000000000.0)
found = False
for s0 in range(-1, 2):
	for s1 in range(-1, 2):
		a = l[0] + s0
		p = l[1] + s1
		diff = p - a
		ok = True
		m = 0
		target = diff + p
		for c in l[2:]:
			dd = (c - target) ** 2
			if dd > 2:
				ok = False
				break
			elif dd > 0:
				m += 1
			target += diff
		if ok:
			mi = min(mi, m + s0 ** 2 + s1 ** 2)
			found = True
print(mi if found else -1)


from sys import exit
n = int(input())
a = list(map(int, input().split()))
if len(a) <= 2:
	print(0)
	exit(0)
diff = [a[i] - a[i - 1] for i in range(1, n)]
variants = set(range(diff[0] - 2, diff[0] + 3))
for j in range(n - 1):
	variants &= set(range(diff[j] - 2, diff[j] + 3))
if len(variants) == 0:
	print(-1)
else:
	mink = 10000000000.0
	for aim in variants:
		p = aim - diff[0]
		if p == 2:
			moves = [-1]
		elif p == 1:
			moves = [-1, 0]
		elif p == 0:
			moves = [-1, 0, 1]
		elif p == -1:
			moves = [0, 1]
		elif p == -2:
			moves = [1]
		for move in moves:
			k = 0
			temp = a.copy()
			temp[0] += move
			if move != 0:
				k += 1
			for i in range(1, n):
				delta = -temp[i] + temp[i - 1] + aim
				if abs(delta) > 1:
					break
				assert abs(delta) <= 1, f'delta: {delta}, move: {move}, aim: {aim}'
				temp[i] += delta
				if delta != 0:
					k += 1
			else:
				mink = min(k, mink)
	if mink == 10000000000.0:
		print(-1)
	else:
		print(mink)


n = int(input())
l = list(map(int, input().split()))
ans = 10 ** 9 + 7
if n < 3:
	print(0)
else:
	ans = 10 ** 9 + 7
	for i in range(l[0] - 1, l[0] + 2):
		for j in range(l[1] - 1, l[1] + 2):
			temp = abs(l[0] - i) + abs(l[1] - j)
			dif = j - i
			cu = j
			flag = 1
			for jj in l[2:]:
				cu += dif
				temp += abs(jj - cu)
				if abs(jj - cu) >= 2:
					flag = 0
			if flag:
				ans = min(ans, temp)
	print(ans) if ans != 10 ** 9 + 7 else print(-1)


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()


n = int(input())
b = input().split()
for i in range(n):
	b[i] = int(b[i])
if n == 1 or n == 2:
	print(0)
else:
	ls = []
	if (b[2] - b[0]) % 2 == 0:
		d = (b[2] - b[0]) // 2
		change = 0
		flag = True
		for i in range(1, n):
			if b[i] - b[0] - d * i == 0:
				continue
			elif b[i] - b[0] - d * i == -1 or b[i] - b[0] - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] - 1) - d * i == 0:
				continue
			elif b[i] - (b[0] - 1) - d * i == -1 or b[i] - (b[0] - 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] + 1) - d * i == 0:
				continue
			elif b[i] - (b[0] + 1) - d * i == -1 or b[i] - (b[0] + 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		d = (b[2] - b[0]) // 2 - 1
		change = 0
		flag = True
		for i in range(1, n):
			if b[i] - b[0] - d * i == 0:
				continue
			elif b[i] - b[0] - d * i == -1 or b[i] - b[0] - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] - 1) - d * i == 0:
				continue
			elif b[i] - (b[0] - 1) - d * i == -1 or b[i] - (b[0] - 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] + 1) - d * i == 0:
				continue
			elif b[i] - (b[0] + 1) - d * i == -1 or b[i] - (b[0] + 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		d = (b[2] - b[0]) // 2 + 1
		change = 0
		flag = True
		for i in range(1, n):
			if b[i] - b[0] - d * i == 0:
				continue
			elif b[i] - b[0] - d * i == -1 or b[i] - b[0] - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] - 1) - d * i == 0:
				continue
			elif b[i] - (b[0] - 1) - d * i == -1 or b[i] - (b[0] - 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] + 1) - d * i == 0:
				continue
			elif b[i] - (b[0] + 1) - d * i == -1 or b[i] - (b[0] + 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
	else:
		d = (b[2] + 1 - b[0]) // 2
		change = 0
		flag = True
		for i in range(1, n):
			if b[i] - b[0] - d * i == 0:
				continue
			elif b[i] - b[0] - d * i == -1 or b[i] - b[0] - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] - 1) - d * i == 0:
				continue
			elif b[i] - (b[0] - 1) - d * i == -1 or b[i] - (b[0] - 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] + 1) - d * i == 0:
				continue
			elif b[i] - (b[0] + 1) - d * i == -1 or b[i] - (b[0] + 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		d = (b[2] - 1 - b[0]) // 2
		change = 0
		flag = True
		for i in range(1, n):
			if b[i] - b[0] - d * i == 0:
				continue
			elif b[i] - b[0] - d * i == -1 or b[i] - b[0] - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] - 1) - d * i == 0:
				continue
			elif b[i] - (b[0] - 1) - d * i == -1 or b[i] - (b[0] - 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
		change = 1
		flag = True
		for i in range(1, n):
			if b[i] - (b[0] + 1) - d * i == 0:
				continue
			elif b[i] - (b[0] + 1) - d * i == -1 or b[i] - (b[0] + 1) - d * i == 1:
				change += 1
			else:
				flag = False
				break
		if flag:
			ls.append(change)
	if len(ls):
		print(min(ls))
	else:
		print(-1)


a = input()
a = [int(i) for i in input().split()]
if len(a) == 1:
	print(0)
	exit(0)
d = (a[-1] - a[0]) // (len(a) - 1)
best = 1000000000
for delta in range(d - 3, d + 3):
	for st in range(a[0] - 1, a[0] + 2):
		c = st
		t = 0
		for i in range(len(a)):
			if abs(a[i] - c) >= 2:
				t = 1000000000
				break
			else:
				t += abs(a[i] - c)
			c = c + delta
		best = min(best, t)
if best == 1000000000:
	best = -1
print(best)


length = int(input())
l = input().split(' ')
l = [int(e) for e in l]
assert length == len(l)
if len(l) < 3:
	print(0)
	exit()
result = -1
for d0 in [-1, 0, 1]:
	for d1 in [-1, 0, 1]:
		for d2 in [-1, 0, 1]:
			step = l[1] + d1 - (l[0] + d0)
			if step != l[2] + d2 - (l[1] + d1):
				continue
			count = 0
			for d in [d0, d1, d2]:
				if d != 0:
					count += 1
			pre = l[2] + d2
			for e in l[3:]:
				d = e - pre - step
				if d > 1 or d < -1:
					count = -1
					break
				if d != 0:
					count += 1
				pre = e - d
			if count == -1:
				continue
			if result == -1 or result > count:
				result = count
print(result)


n = int(input())
b = list(map(int, input().split()))
l = len(b)
answer = False
best = 1000000000.0
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
				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 != 1000000000.0:
		print(best)
	else:
		print(-1)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a = list(map(int, input().split()))
d = list()
for i in range(n - 1):
	d.append(a[i + 1] - a[i])
if n <= 2:
	print(0)
else:
	mn = n + 2
	for i in range(-1, 2):
		for j in range(-1, 2):
			b = list(a)
			b[0] += i
			b[1] += j
			diff = b[1] - b[0]
			ct = 0
			if i != 0:
				ct += 1
			if j != 0:
				ct += 1
			for k in range(2, n):
				nw = b[k] - b[k - 1] - diff
				if nw < -1 or nw > 1:
					ct = -1
					break
				if nw != 0:
					b[k] -= nw
					ct += 1
			if ct >= 0:
				mn = min(mn, ct)
	if mn == n + 2:
		mn = -1
	print(mn)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 1000000007
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = int(input())
a = list(map(int, input().split(' ')))
diff = []
for x in range(1, n):
	diff.append(a[x] - a[x - 1])
if n == 1:
	print(0)
else:
	hp = list(a)
	ans = 1e+100
	tochange = [1, 0, -1]
	for x in range(3):
		for y in range(3):
			a[0] += tochange[x]
			a[1] += tochange[y]
			diff = a[1] - a[0]
			chk = 0
			cnt = abs(tochange[x]) + abs(tochange[y])
			for z in range(2, n):
				if abs(a[z] - a[z - 1] - diff) <= 1:
					cnt += abs(a[z] - a[z - 1] - diff)
					a[z] = a[z - 1] + diff
				else:
					chk = 1
					break
			a = list(hp)
			if chk == 1:
				continue
			else:
				ans = min(ans, cnt)
	print(-1 if ans == 1e+100 else ans)


R = lambda : map(int, input().split())
inf = 10 ** 7
n = int(input())
b = list(R())

def f(a):
	r = 0
	d = a[1] - a[0]
	e = a[1]
	for i in range(2, n):
		e += d
		if abs(a[i] - e) == 1:
			r += 1
		elif a[i] != e:
			return inf
	return r
if n <= 2:
	print(0)
else:
	r = inf
	for dx in (-1, 0, 1):
		for dy in (-1, 0, 1):
			b[0] += dx
			b[1] += dy
			r = min(r, f(b) + abs(dx) + abs(dy))
			b[0] -= dx
			b[1] -= dy
	if r == inf:
		r = -1
	print(r)


import math
n = int(input())
numbers = list(map(int, input().split()))
rez = -1
if n <= 2:
	print('0')
	exit()
else:
	for i in range(-1, 2):
		for j in range(-1, 2):
			x = j - i + numbers[1] - numbers[0]
			fin = abs(i) + abs(j)
			for k in range(2, n):
				y = abs(numbers[k] - numbers[1] - j - x * (k - 1))
				if y > 1:
					fin = -1
					break
				else:
					fin = fin + y
			if fin >= 0:
				rez = min(rez, fin) if rez >= 0 else fin
	print(rez)


import sys

def main():
	n = int(input())
	if n < 3:
		return print(0)
	nums = [int(num) for num in input().strip().split(' ')]
	(first, last) = (nums[0], nums[-1])
	res = -1
	for start in [first - 1, first, first + 1]:
		for end in [last - 1, last, last + 1]:
			step = (end - start) / (len(nums) - 1)
			if step % 1 != 0:
				continue
			cur = start
			cur_res = 0
			for num in nums:
				diff = num - cur
				if abs(diff) > 1:
					cur_res = -1
					break
				else:
					cur_res += abs(diff)
				cur += step
			if res == -1 and cur_res != -1:
				res = cur_res
			if cur_res != -1 and res != -1 and (cur_res < res):
				res = cur_res
	return print(int(res))
sys.exit(main())


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	n = int(inp())
	a = lis()
	diff = []
	if n < 3:
		print(0)
		return
	for i in range(1, n):
		diff.append(a[i] - a[i - 1])
	(chota, bada) = (min(diff), max(diff))
	if bada - chota > 4:
		print(-1)
		return
	ans = 10 ** 9
	d = [-1, 0, +1]
	for i in range(chota, bada + 1):
		if i - chota > 2 or bada - i > 2:
			continue
		for j in range(0, 3):
			cnt = 0
			f = a[0] + d[j]
			flag = True
			for k in range(0, n):
				if abs(f + k * i - a[k]) > 1:
					flag = False
					break
				cnt += abs(f + k * i - a[k])
			if flag:
				ans = min(cnt, ans)
	if ans == 10 ** 9:
		print(-1)
	else:
		print(ans)
testcase(1)


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:
				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 f(n, d, l):
	k = 1
	z = n
	ans = 0
	while k < len(l):
		if int(l[k]) - z == d:
			z = int(l[k])
			ans = ans + 0
		elif int(l[k]) - z + 1 == d:
			z = int(l[k]) + 1
			ans = ans + 1
		elif int(l[k]) - z - 1 == d:
			z = int(l[k]) - 1
			ans = ans + 1
		else:
			return -1
		k = k + 1
	return ans
n = int(input())
l = input().split()
if n <= 2:
	print('0')
elif n == 3 or n == 4:
	if n == 3:
		ans = 5
		(l[0], l[1], l[2]) = (int(l[0]), int(l[1]), int(l[2]))
		a = -1
		while a <= 1:
			b = -1
			while b <= 1:
				c = -1
				while c <= 1:
					(q, w, e) = (l[0] + a, l[1] + b, l[2] + c)
					if q - w == w - e:
						ans = min(ans, abs(a) + abs(b) + abs(c))
					c = c + 1
				b = b + 1
			a = a + 1
	if n == 4:
		ans = 5
		(l[0], l[1], l[2], l[3]) = (int(l[0]), int(l[1]), int(l[2]), int(l[3]))
		a = -1
		while a <= 1:
			b = -1
			while b <= 1:
				c = -1
				while c <= 1:
					d = -1
					while d <= 1:
						(q, w, e, r) = (l[0] + a, l[1] + b, l[2] + c, l[3] + d)
						if q - w == w - e & w - e == e - r:
							ans = min(ans, abs(a) + abs(b) + abs(c) + abs(d))
						d = d + 1
					c = c + 1
				b = b + 1
			a = a + 1
	if ans == 5:
		print('-1')
	else:
		print(ans)
else:
	a = int(l[0])
	b = int(l[-1])
	d = 0
	if (b - a) % (n - 1) == 0:
		d = (b - a) // (n - 1)
	if (b - a + 1) % (n - 1) == 0:
		d = (b - a + 1) // (n - 1)
	if (b - a + 2) % (n - 1) == 0:
		d = (b - a + 2) // (n - 1)
	if (b - a - 1) % (n - 1) == 0:
		d = (b - a - 1) // (n - 1)
	if (b - a - 2) % (n - 1) == 0:
		d = (b - a - 2) // (n - 1)
	q = int(l[0])
	(f1, f2, f3) = (f(q - 1, d, l), f(q, d, l), f(q + 1, d, l))
	if f1 == f2 & f2 == f3 & f3 == -1:
		print('-1')
	else:
		if f1 == -1:
			f1 = max(f2, f3) + 1
		if f2 == -1:
			f2 = max(f1, f3) + 1
		if f3 == -1:
			f3 = max(f1, f2) + 2
		print(min(f1 + 1, f2, f3 + 1))


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
	for x in nums:
		s = pre - x
		if abs(step - s) > 1:
			raise Exception()
		if step == s:
			pre = x
			continue
		suma += 1
		if pre - (x + 1) == step:
			pre = x + 1
		else:
			pre = x - 1
	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:
			suma = min(suma, check(nums[2:], diff, b) + abs(x) + abs(y))
		except Exception as e:
			pass
	if suma == 100000000:
		print(-1)
	else:
		print(suma)
main()


n = int(input())
arr = list(map(int, input().split()))
if n <= 2:
	print('0')
else:
	res = int(-1)
	for i in range(-1, 2):
		for j in range(-1, 2):
			arr_cp = arr[:]
			arr_cp[0] = arr_cp[0] + i
			arr_cp[1] = arr_cp[1] + j
			diff = arr_cp[1] - arr_cp[0]
			ct = abs(j) + abs(i)
			ok = 1
			for k in range(2, n):
				if abs(arr_cp[k] - arr_cp[k - 1] - diff) > 1:
					ok = 0
					break
				else:
					arr_cp[k] = arr_cp[k - 1] + diff
					if arr_cp[k] != arr[k]:
						ct = ct + 1
			if ok == 1:
				if res == -1 or res > ct:
					res = ct
	print(res)


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

def checkAP(begin, end, n):
	diff = end - begin
	step = int(diff / (n - 1))
	if step * (n - 1) != diff:
		return 0.5
	else:
		return step

def checkPossible(AP, A):
	total = 0
	for (ap, a) in zip(AP, A):
		if a == ap - 1 or a == ap + 1:
			total += 1
		elif a == ap:
			pass
		else:
			return -1
	return total

def check(a, n):
	if n == 1 or n == 2:
		return 0
	begin = [a[0] - 1, a[0], a[0] + 1]
	end = [a[-1] - 1, a[-1], a[-1] + 1]
	pairs = []
	[[pairs.append((be, e)) for e in end] for be in begin]
	curr = n + 1
	for (begin, end) in pairs:
		step = checkAP(begin, end, n)
		if step != 0.5:
			ap = [begin + step * i for i in range(n)]
			total = checkPossible(ap, a)
			if total == -1:
				pass
			elif total == 0:
				return 0
			elif total < curr:
				curr = total
	if curr == n + 1:
		return -1
	else:
		return curr
print(check(a=a, n=n))


from fractions import gcd
from heapq import *
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions
MOD = 10 ** 9 + 7

def main():
	n = int(input())
	b = list(map(int, input().split()))
	if n <= 2:
		print(0)
		exit()
	ans = 10 ** 18
	for i in [-1, 0, 1]:
		for j in [-1, 0, 1]:
			sa = []
			a = []
			for t in range(n):
				a.append(b[t])
			a[0] += i
			a[1] += j
			v = a[0] - a[1]
			cnt = 0
			if i != 0:
				cnt += 1
			if j != 0:
				cnt += 1
			for t in range(1, n - 1):
				if v == a[t] - a[t + 1]:
					continue
				elif a[t] - a[t + 1] > v:
					a[t + 1] += 1
					cnt += 1
				else:
					a[t + 1] -= 1
					cnt += 1
				if a[t] - a[t + 1] != v:
					cnt = 10 ** 18
					break
			ans = min(ans, cnt)
	if ans == 10 ** 18:
		print(-1)
	else:
		print(ans)
main()


n = int(input())
b = list(map(int, input().split()))
a = [0] * n
ans = -1
if n <= 2:
	ans = 0
else:
	for d1 in range(-1, 2, 1):
		for d2 in range(-1, 2, 1):
			a[0] = b[0] + d1
			a[1] = b[1] + d2
			d = a[1] - a[0]
			cnt = int(d1 != 0) + int(d2 != 0)
			i = 1
			while i < n - 1:
				if abs(b[i + 1] - (a[i] + d)) > 1:
					break
				else:
					a[i + 1] = a[i] + d
					if abs(b[i + 1] - (a[i] + d)) != 0:
						cnt += 1
				i += 1
			if i == n - 1:
				if ans == -1:
					ans = cnt
				else:
					ans = min(ans, cnt)
print(ans)


n = int(input())
a = list(map(int, input().split()))
if n <= 2:
	print(0)
	exit(0)
INF = 10 ** 6
ans = INF
for i in range(-1, 2):
	for j in range(-1, 2):
		b = a[:]
		b[0] += i
		b[1] += j
		d = b[1] - b[0]
		tmp = abs(i) + abs(j)
		for k in range(1, n - 1):
			if abs(b[k + 1] - b[k] - d) > 1:
				break
			tmp += abs(b[k + 1] - b[k] - d)
			b[k + 1] = b[k] + d
		else:
			ans = min(ans, tmp)
if ans < INF:
	print(ans)
else:
	print(-1)


inf = 4557430888798830399
n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
r = []
for x in [-1, 0, 1]:
	for y in [-1, 0, 1]:
		f = 1
		t = 0
		s = a[0] + x
		d = a[1] + y - s
		b = range(s, s + n * d, d) if d else [s] * n
		for i in range(n):
			p = abs(b[i] - a[i])
			if p > 1:
				r.append(inf)
				f = 0
				break
			if p == 1:
				t += 1
		if f:
			r.append(t)
r = min(r)
print(-1 if r == inf else r)


n = int(input())
ax = list(map(int, input().split()))
if n <= 2:
	print(0)
	quit()
if ax[0] > ax[1]:
	a = ax[::-1]
mindiff = ax[1] - ax[0]
maxdiff = ax[1] - ax[0]
for i in range(1, n):
	mindiff = min(mindiff, ax[i] - ax[i - 1])
	maxdiff = max(maxdiff, ax[i] - ax[i - 1])
if maxdiff - mindiff > 4:
	print(-1)
	quit()
if maxdiff == mindiff:
	print(0)
	quit()
a = ax
lis = []
for i in range(-1, 2):
	for j in range(-1, 2):
		another = list(a)
		another[0] += i
		another[1] += j
		x = another[0]
		y = another[1]
		diff = y - x
		count = 0
		if i != 0:
			count += 1
		if j != 0:
			count += 1
		poss = True
		for k in range(2, n):
			diffdiff = diff - (another[k] - another[k - 1])
			if not -1 <= diffdiff <= 1:
				poss = False
				break
			elif diffdiff != 0:
				count += 1
				another[k] += diffdiff
		if poss:
			lis.append(count)
if len(lis) == 0:
	print(-1)
else:
	print(min(lis))


n = int(input())
a = list(map(int, input().split()))
if n <= 2:
	print(0)
	exit(0)
from itertools import product
s = [-1, 0, 1]
mo = []
for (s1, s2) in list(product(s, s)):
	(a0, an) = (a[0] + s1, a[-1] + s2)
	(d, m) = divmod(an - a0, n - 1)
	if m > 0:
		continue
	mc = 0
	np = False
	for i in range(n - 2):
		ex = a0 + (i + 1) * d
		ac = a[i + 1]
		diff = abs(ac - ex)
		if diff > 1:
			np = True
			break
		elif diff == 1:
			mc += 1
	if not np:
		mc += abs(s1) + abs(s2)
		mo.append(mc)
		if mc == 0:
			break
if len(mo) == 0:
	print(-1)
else:
	print(min(mo))


def changes(l, n, a, b):
	d = b - a
	output = 0
	for i in range(n):
		off = abs(a + i * d - l[i])
		if off > 1:
			return 'No'
		elif off == 1:
			output += 1
	return output
n = int(input())
l = [int(x) for x in input().split()]
if n <= 2:
	print(0)
else:
	options = []
	u = l[0]
	v = l[1]
	for j in [u - 1, u, u + 1]:
		for k in [v - 1, v, v + 1]:
			c = changes(l, n, j, k)
			if c != 'No':
				options.append(c)
	if len(options) == 0:
		print(-1)
	else:
		print(min(options))


n = int(input())
a = list(map(int, input().split()))
if n < 3:
	print(0)
	exit()
ans = n + 1
for d1 in range(-1, 2):
	for d2 in range(-1, 2):
		(c, a1, a2) = (0, a[0] + d1, a[1] + d2)
		k = a2 - a1
		if d1 != 0:
			c += 1
		if d2 != 0:
			c += 1
		for i in range(2, n):
			ai = a1 + i * k
			if abs(a[i] - ai) > 1:
				c = -1
				break
			if a[i] == ai:
				continue
			c += 1
		if c == -1:
			continue
		if c < ans:
			ans = c
if ans > n:
	print(-1)
else:
	print(ans)


import sys
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
N = INT()
A = LIST()
if N <= 2:
	print(0)
	exit()

def check(a, b):
	dist = b - a
	cur = b
	res = abs(a - A[0]) + abs(b - A[1])
	for i in range(2, N):
		cur += dist
		if cur < A[i] - 1 or cur > A[i] + 1:
			return INF
		res += abs(cur - A[i])
	return res
ans = INF
for i in range(A[0] - 1, A[0] + 2):
	for j in range(A[1] - 1, A[1] + 2):
		ans = min(ans, check(i, j))
if ans == INF:
	print(-1)
else:
	print(ans)


import sys
import math

def intcheck(a, b, n):
	n = n - 1
	if math.ceil(abs(a - b) / n) == math.floor(abs(a - b) / n):
		return True
	return False

def creata(a, b, n):
	n = n - 1
	if a > b:
		dist = (a - b) // n
		rta = [a - int(x) * dist for x in range(n + 1)]
	else:
		dist = (b - a) // n
		rta = [a + int(x) * dist for x in range(n + 1)]
	return rta

def lische(lista, rta):
	cnta = 0
	for i in range(len(rta)):
		if abs(lista[i] - rta[i]) != 1 and abs(lista[i] - rta[i]) != 0:
			cnta = -1
			break
		elif abs(lista[i] - rta[i]) == 1:
			cnta += 1
	return cnta
n = int(input())
lista = [int(x) for x in input().strip().split()]
store = []
mina = 1e+16
if n == 1:
	print('0')
else:
	le = [lista[0], lista[0] - 1, lista[0] + 1]
	rg = [lista[-1], lista[-1] - 1, lista[-1] + 1]
	val = 1
	flat = False
	for l in le:
		for r in rg:
			if intcheck(l, r, n):
				val = lische(lista, creata(l, r, n))
				if val != -1:
					store.append(val)
					mina = min(mina, val)
	if len(store) == 0:
		print('-1')
	else:
		print(mina)


import copy
num = int(input())
enter = list(map(int, input().split(' ')))

def main():
	if num <= 2:
		print(0)
		return
	answer = 10 ** 13
	for i in [-1, 0, 1]:
		for j in [-1, 0, 1]:
			temp = copy.deepcopy(enter)
			change = 0
			if i != 0:
				change += 1
			if j != 0:
				change += 1
			temp[0] += i
			temp[1] += j
			diff = temp[1] - temp[0]
			impossible = False
			k = 2
			while k < num and (not impossible):
				if enter[k] == temp[k - 1] + diff:
					k += 1
					continue
				elif abs(enter[k] - temp[k - 1] - diff) <= 1:
					change += 1
					temp[k] = temp[k - 1] + diff
				else:
					impossible = True
				k += 1
			if not impossible:
				answer = min(answer, change)
	if answer == 10 ** 13:
		print(-1)
	else:
		print(answer)
main()


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

def f(x, y):
	b = a.copy()
	b[0] += x
	b[1] += y
	step = b[1] - b[0]
	ans = abs(x) + abs(y)
	for i in range(2, n):
		f = b[i - 1] + step
		ch = abs(f - b[i])
		if ch > 1:
			return inf
		ans += ch
		b[i] = f
	return ans
if n < 3:
	print(0)
else:
	ans = inf
	for i in (-1, 0, 1):
		for j in (-1, 0, 1):
			z = f(i, j)
			ans = min(ans, z)
	if ans == inf:
		print(-1)
	else:
		print(ans)


import sys
read = lambda : map(int, sys.stdin.buffer.readline().split())
(n,) = read()
v = list(read())
ans = -1
if n <= 2:
	print(0)
else:
	for a in range(-1, 2):
		for b in range(-1, 2):
			d = b - a + v[1] - v[0]
			res = abs(a) + abs(b)
			for i in range(2, n):
				k = abs(v[i] - v[1] - b - d * (i - 1))
				if k > 1:
					res = -1
					break
				else:
					res += k
			if res >= 0:
				ans = min(ans, res) if ans >= 0 else res
	print(ans)


n = int(input())
a = list(map(int, input().split()))
if n <= 2:
	print(0)
	exit()
ans = 10 ** 10
for d1 in range(-1, 2):
	for d2 in range(-1, 2):
		(temp, a1, a2) = (0, a[0] + d1, a[1] + d2)
		diff = a2 - a1
		for i in range(0, len(a)):
			term = a1 + i * diff
			if abs(term - a[i]) <= 1:
				temp += abs(term - a[i])
			else:
				temp = 10 ** 10
				break
		ans = min(ans, temp)
if ans == 10 ** 10:
	print(-1)
else:
	print(ans)


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] -= 1
if n < 3:
	print('0')
	exit()
ans = 123456
for i in range(3):
	for j in range(3):
		changes = 0
		if i != 1:
			changes += 1
		if j != 1:
			changes += 1
		d = a[1] + j - (a[0] + i)
		prev = a[1] + j
		flag = True
		for k in range(2, n):
			if prev + d == a[k] + 1:
				prev = prev + d
			elif prev + d == a[k] or prev + d == a[k] + 2:
				prev = prev + d
				changes += 1
			else:
				flag = False
				break
		if flag:
			ans = min(ans, changes)
if ans == 123456:
	print('-1')
else:
	print('%d' % ans)


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))


a = int(input())
z = list(map(int, input().split()))
if len(z) == 1 or len(z) == 2:
	print(0)
	exit()
dp = [[0 for i in range(len(z))] for i in range(3)]
for i in range(len(z)):
	dp[0][i] = z[i] + 1
	dp[1][i] = z[i]
	dp[2][i] = z[i] - 1
flag = 0
count = 0
orr = 0
import math
mini = math.inf
for i in range(3):
	a = dp[i][0]
	orr = 0
	if i != 1:
		orr += 1
	count = orr
	for j in range(3):
		if j != 1:
			count += 1
		b = dp[j][1]
		comm = dp[j][1] - dp[i][0]
		t = 2
		while t < len(z):
			if dp[0][t] == comm + b:
				b = dp[0][t]
				count += 1
				t += 1
				continue
			elif dp[1][t] == comm + b:
				b = dp[1][t]
				t += 1
				continue
			elif dp[2][t] == comm + b:
				b = dp[2][t]
				count += 1
				t += 1
				continue
			else:
				break
		if t == len(z):
			flag = 1
			mini = min(mini, count)
		count = orr
if flag == 0:
	print(-1)
if flag == 1:
	print(mini)


n = int(input())
l = list(map(int, input().split()))
if n == 1 or n == 2:
	print(0)
else:
	l1 = [l[0] - 1, l[0], l[0] + 1]
	l2 = [l[1] - 1, l[1], l[1] + 1]
	c = []
	for a in l1:
		for x in l2:
			if a == l[0] and x != l[1]:
				count = 1
			elif a == l[0] and x == l[1]:
				count = 0
			elif a != l[0] and x != l[1]:
				count = 2
			elif a != l[0] and x == l[1]:
				count = 1
			d = x - a
			for i in range(2, n):
				if abs(a + i * d - l[i]) == 1:
					count = count + 1
					if i == n - 1:
						c.append(count)
				elif a + i * d - l[i] == 0:
					count = count
					if i == n - 1:
						c.append(count)
				else:
					break
	if len(c) == 0:
		print(-1)
	else:
		print(min(c))


n = int(input())
b = list(map(int, input().split()))
ans = n * 2
if n == 1:
	print(0)
else:
	for i in range(1, -2, -1):
		first = b[0] + i
		for j in range(1, -2, -1):
			last = b[-1] + j
			diff = last - first
			count = abs(i) + abs(j)
			if diff % (n - 1) == 0:
				a = diff // (n - 1)
				tmp = first + a
				flag = True
				for x in b[1:-1]:
					if abs(x - tmp) <= 1:
						count += abs(x - tmp)
						tmp += a
					else:
						flag = False
						break
				if flag:
					ans = min(ans, count)
	if ans > n:
		ans = -1
	print(ans)


from sys import stdin, stdout
import math
N = int(input())
arr = [int(x) for x in stdin.readline().split()]
if N == 1:
	print(0)
	quit()
a1 = [arr[0] + 1, arr[0], arr[0] - 1]
a2 = [arr[1] + 1, arr[1], arr[1] - 1]
ans = -1
for n1 in a1:
	for n2 in a2:
		res = 0
		if n1 != arr[0]:
			res += 1
		if n2 != arr[1]:
			res += 1
		d = n2 - n1
		v = n2
		for i in range(2, N):
			v += d
			if abs(v - arr[i]) > 1:
				res = -1
				break
			elif v != arr[i]:
				res += 1
		if res != -1 and ans == -1:
			ans = res
		elif res != -1 and ans != -1:
			ans = min(ans, res)
if ans == -1:
	print(-1)
else:
	print(ans)


n = int(input())
nums = list(map(int, input().split()))
if n == 1:
	print(0)
else:
	poss = [nums[0] - nums[1] + x for x in range(-2, 3)]
	res = []
	for dif in poss:
		for y in range(-1, 2):
			cnt = 0
			for x in range(n):
				if abs(nums[x] - y - nums[0] + x * dif) > 1:
					break
				elif abs(nums[x] - y - nums[0] + x * dif) == 1:
					cnt += 1
			else:
				res.append(cnt)
	if res:
		print(min(res))
	else:
		print(-1)


def simulate(ar, moves):
	a = ar.copy()
	n = len(a)
	diff = a[1] - a[0]
	for i in range(1, n):
		if a[i] - a[i - 1] != diff:
			if a[i] - a[i - 1] == diff + 1:
				a[i] -= 1
			elif a[i] - a[i - 1] == diff - 1:
				a[i] += 1
			else:
				return 10 ** 18
			moves += 1
	return moves
moves = 10 ** 18
n = int(input())
ar = list(map(int, input().split()))
if n <= 2:
	print(0)
else:
	moves = min(moves, simulate(ar, 0))
	moves = min(moves, simulate([ar[0]] + [ar[1] + 1] + ar[2:], 1))
	moves = min(moves, simulate([ar[0]] + [ar[1] - 1] + ar[2:], 1))
	moves = min(moves, simulate([ar[0] + 1] + ar[1:], 1))
	moves = min(moves, simulate([ar[0] + 1] + [ar[1] - 1] + ar[2:], 2))
	moves = min(moves, simulate([ar[0] + 1] + [ar[1] + 1] + ar[2:], 2))
	moves = min(moves, simulate([ar[0] - 1] + ar[1:], 1))
	moves = min(moves, simulate([ar[0] - 1] + [ar[1] - 1] + ar[2:], 2))
	moves = min(moves, simulate([ar[0] - 1] + [ar[1] + 1] + ar[2:], 2))
	print(-1 if moves == 10 ** 18 else moves)


import sys
n = int(input())
a = [int(x) for x in input().split()]
if len(a) < 3:
	print(0)
	sys.exit()
MAX = 10 ** 9

def check(diff, start):
	global MAX
	ans = 0
	for i in range(2, n):
		shall = start + i * diff
		d = abs(a[i] - shall)
		if d > 1:
			return MAX
		ans += d
	return ans
ans = MAX
for i in range(-1, 2):
	for j in range(-1, 2):
		a = list(a)
		start = a[0] + i
		second = a[1] + j
		d = second - start
		cur = abs(i) + abs(j) + check(d, start)
		ans = min(cur, ans)
if ans == MAX:
	print(-1)
else:
	print(ans)


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)


n = int(input())
a = list(map(int, input().split()))
if n < 3:
	print(0)
	exit()
dif = a[1] - a[0]
ans = 9999999
for x in range(-1, 2, 1):
	for y in range(-1, 2, 1):
		maybe = True
		cur = a.copy()
		cur_ans = 0
		cur[0] += x
		cur[1] += y
		dif = cur[1] - cur[0]
		for pos in range(2, n):
			if abs(cur[pos] - cur[pos - 1] - dif) < 2:
				cur_ans += abs(cur[pos] - cur[pos - 1] - dif)
				cur[pos] = cur[pos - 1] + dif
			else:
				maybe = False
		if maybe:
			ans = min(ans, cur_ans + abs(x) + abs(y))
if ans == 9999999:
	print(-1)
else:
	print(ans)


import math
from collections import defaultdict
from sys import stdin, stdout
input = stdin.readline

def main():
	n = int(input())
	a = list(map(int, input().split()))
	if len(a) < 3:
		print(0)
	else:
		ans = n + 1
		for d1 in (-1, 0, 1):
			for d2 in (-1, 0, 1):
				a0 = a[0] + d1
				a1 = a[1] + d2
				d = a1 - a0
				ok = True
				change = int(d1 != 0) + int(d2 != 0)
				prev = a1
				for i in range(2, n):
					cand = prev + d
					if abs(cand - a[i]) > 1:
						ok = False
						break
					else:
						change += int(cand != a[i])
						prev = cand
				if ok:
					ans = min(ans, change)
		if ans == n + 1:
			print(-1)
		else:
			print(ans)
main()


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, seq[0] + x - seq[1] - y, int(x != 0) + int(y != 0))
			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())
a = list(map(int, input().split()))

def check(d, start):
	b = start
	i = 0
	cnt = 0
	for i in range(n):
		if abs(a[i] - b) > 1:
			break
		cnt += a[i] != b
		b += d
		i += 1
	if i == n:
		return (True, cnt)
	else:
		return (False, -1)

def run():
	if n == 1:
		print(0)
		return
	total_diff = a[n - 1] - a[0]
	min_changes = -1
	for lshift in (-1, 0, 1):
		for rshift in (-1, 0, 1):
			total_shift = rshift - lshift
			new_diff = total_shift + total_diff
			if abs(new_diff) % (n - 1) == 0:
				d = new_diff // (n - 1)
				(ok, changes) = check(d, a[0] + lshift)
				if ok:
					min_changes = changes if min_changes == -1 else min(changes, min_changes)
	print(min_changes)
run()


import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')

def st():
	return list(sys.stdin.readline().strip())

def li():
	return list(map(int, sys.stdin.readline().split()))

def mp():
	return map(int, sys.stdin.readline().split())

def inp():
	return int(sys.stdin.readline())

def pr(n):
	return sys.stdout.write(str(n) + '\n')

def prl(n):
	return sys.stdout.write(str(n) + ' ')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():

	def help(i, j):
		x = list(l)
		x[0] += i
		x[1] += j
		d = x[1] - x[0]
		cur = abs(i) + abs(j)
		for k in range(2, n):
			v = x[k] - x[k - 1]
			if abs(d - v) > 1:
				return INF
			x[k] += d - v
			cur += abs(d - v)
		return cur
	n = inp()
	l = li()
	if n < 3:
		pr(0)
		return
	ans = INF
	for i in range(-1, 2):
		for j in range(-1, 2):
			ans = min(ans, help(i, j))
	if ans == INF:
		ans = -1
	pr(ans)
for _ in range(1):
	solve()


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	if y == 0:
		return 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

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

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt
from decimal import Decimal

def solve():
	n = scanner()
	if n <= 2:
		print(0)
		return
	b = get_list()
	storecnt = []
	for d1 in range(-1, 2):
		for d2 in range(-1, 2):
			a = b.copy()
			a[0] = a[0] + d1
			a[1] = a[1] + d2
			cnt = abs(d1) + abs(d2)
			d = a[1] - a[0]
			for i in range(2, n):
				d_ = a[i] - a[i - 1]
				if abs(d_ - d) <= 1:
					a[i] += d - d_
					cnt += abs(d - d_)
				else:
					break
			else:
				storecnt.append(cnt)
	if len(storecnt) == 0:
		print(-1)
	else:
		print(min(storecnt))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = 1
for i in range(t):
	solve()


from collections import Counter, defaultdict
BS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def to_base(s, b):
	res = ''
	while s:
		res += BS[s % b]
		s //= b
	return res[::-1] or '0'
alpha = 'abcdefghijklmnopqrstuvwxyz'
from math import floor, ceil, pi
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919]

def primef(n, plst=[]):
	if n == 1:
		return plst
	else:
		for m in primes:
			if n % m == 0:
				return primef(n // m, plst + [m])
		return primef(1, plst + [n])

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

def ii():
	return int(input())

def countOverlapping(string, sub):
	count = start = 0
	while True:
		start = string.find(sub, start) + 1
		if start > 0:
			count += 1
		else:
			return count
from copy import deepcopy
n = ii()
cnums = lmii()
ans = 9999999999
found = False
for start1 in [0, -1, 1]:
	for start2 in [0, -1, 1]:
		if len(cnums) == 1:
			found = True
			ans = 0
			continue
		nums = deepcopy(cnums)
		nums[0] += start1
		nums[1] += start2
		dif = nums[0] - nums[1]
		can = True
		tot = 0
		for i in range(1, len(nums)):
			if nums[i - 1] - nums[i] - dif == 1:
				tot += 1
				nums[i] += 1
			elif nums[i - 1] - nums[i] - dif == -1:
				nums[i] -= 1
				tot += 1
			elif nums[i - 1] - nums[i] - dif == 0:
				continue
			else:
				can = False
				break
		if can:
			ans = min(abs(start1) + abs(start2) + tot, ans)
			found = True
if not found:
	print(-1)
else:
	print(ans)


n = int(input())
if n < 2:
	exit(print(0))
t = list(map(int, input().split()))
(k, s) = (n - 1, n + 1)
d = t[k] - t[0]
for d in range((d - 2) // k, (d + 2) // k + 1):
	for i in (-1, 0, 1):
		p = [abs(t[0] + i + d * k - q) for (k, q) in enumerate(t)]
		if max(p) < 2:
			s = min(s, sum(p))
print(-1 if s > n else s)


n = int(input())
aa = input()
a = [int(s) for s in aa.split(' ')]
b = []
if n < 3:
	print(0)
else:
	all_ok = True
	for i in range(-1, 2):
		for j in range(-1, 2):
			a1 = a[0] + i
			a2 = a[1] + j
			el = {'a': a2, 'd': a2 - a1, 'izm': abs(i) + abs(j), 'ok': True}
			b.append(el)
	for i in range(2, n):
		ai = a[i]
		for el in b:
			if not el['ok']:
				continue
			d = ai - el['a'] - el['d']
			if abs(d) > 1:
				el['ok'] = False
			elif abs(d) == 1:
				el['izm'] = el['izm'] + 1
		b = [x for x in b if x['ok']]
		if len(b) == 0:
			all_ok = False
			break
		for el in b:
			el['a'] = el['a'] + el['d']
	if not all_ok:
		print(-1)
	else:
		print(min([x['izm'] for x in b]))


def func(a, b):
	c = 0
	for i in range(len(a)):
		if abs(a[i] - b[i]) == 1:
			c = c + 1
		elif abs(a[i] - b[i]) > 1:
			return -1
	return c
n = int(input())
a = list(map(int, input().split()))
if n < 3:
	print(0)
else:
	b = []
	f = []
	for i in [a[0] - 1, a[0], a[0] + 1]:
		for j in [a[1] - 1, a[1], a[1] + 1]:
			c = [i]
			d = j - i
			for k in range(n - 1):
				c.append(c[-1] + d)
			e = func(a, c)
			if e != -1:
				f.append(e)
	if len(f) != 0:
		print(min(f))
	else:
		print(-1)


n = int(input())
b = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
globalArr = []
for k in (1, 0, -1):
	for l in (1, 0, -1):
		(a1, a2) = (b[0] + k, b[1] + l)
		cnt = 0
		flag = True
		for j in range(n):
			ai = a1 + j * (a2 - a1)
			if abs(ai - b[j]) <= 1:
				cnt += abs(ai - b[j])
			else:
				flag = False
				break
		if flag:
			globalArr.append(cnt)
if globalArr:
	print(min(globalArr))
else:
	print(-1)


import sys
n = int(input())
list1 = list(map(int, input().split()))
f = 0
ff = 0
cnt = 0
arr = []
if n > 2:
	xx = list1[0]
	yy = list1[1]
	ll = list1.copy()
	for i in range(-1, 2):
		for j in range(-1, 2):
			list1 = ll.copy()
			cnt = 0
			list1[0] = xx + i
			if i != 0:
				cnt += 1
			list1[1] = yy + j
			if j != 0:
				cnt += 1
			d = list1[1] - list1[0]
			f = 0
			for ii in range(2, n):
				if list1[ii] - list1[ii - 1] == d:
					continue
				elif list1[ii] - list1[ii - 1] == d + 1:
					list1[ii] -= 1
					cnt += 1
				elif list1[ii] - list1[ii - 1] == d - 1:
					list1[ii] += 1
					cnt += 1
				else:
					f = 1
			if f == 0:
				arr.append(cnt)
	if arr:
		print(min(arr))
	else:
		print(-1)
else:
	print(0)


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))


n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
diff = (a[-1] - a[0]) // (n - 1)
diff_candidate = [diff - 2, diff - 1, diff, diff + 1, diff + 2]
ans = 10 ** 18 + 7
for d in diff_candidate:
	for i in [-1, 0, 1]:
		num = a[0] - d + i
		tmp_ans = 0
		for next_num in a:
			if next_num - num == d:
				num = next_num
			elif next_num - num == d + 1:
				num = next_num - 1
				tmp_ans += 1
			elif next_num - num == d - 1:
				num = next_num + 1
				tmp_ans += 1
			else:
				break
		else:
			ans = min(ans, tmp_ans)
if ans == 10 ** 18 + 7:
	print(-1)
else:
	print(ans)


import sys
from bisect import bisect_left, bisect_right
from collections import deque, Counter
from math import gcd, sqrt, factorial, ceil, log
from itertools import permutations
inf = float('inf')
mod = 1000000007
mini = 1000000007

def fact(n):
	if n == 0:
		return 1
	p = 1
	d = 10 ** 9 + 7
	for i in range(1, n + 1):
		p = p * i
		p = p % d
	return p

def ncr(n, r):
	d = 10 ** 9 + 7
	num = fact(n)
	den = fact(r) * fact(n - r) % d
	den = pow(den, d - 2, d)
	return num * den % d

def sieve(n):
	prime = [True for i in range(n + 1)]
	lst = [0] * (n + 1)
	p = 2
	while p * p <= n:
		if prime[p]:
			lst[p] = p
			for i in range(p * p, n + 1, p):
				if prime[i] == True:
					prime[i] = False
					lst[i] = p
		p = p + 1
	for i in range(1, n + 1):
		if lst[i] == 0:
			lst[i] = i
	mylist = [0] * (n + 1)
	for i in range(2, n + 1):
		mylist[lst[i]] = mylist[lst[i]] + 1
	return mylist

def binary(number):
	result = 0
	while number:
		result = result + 1
		number = number & number - 1
	return result

def calculate_factors(n):
	hh = [1] * (n + 1)
	p = 2
	while p * p < n:
		if hh[p] == 1:
			for i in range(p * 2, n, p):
				hh[i] = 0
		p += 1
	total = 1
	for p in range(2, n + 1):
		if hh[p] == 1:
			count = 0
			if n % p == 0:
				while n % p == 0:
					n = int(n / p)
					count += 1
				total *= count + 1
	return total

def prime_factors(n):
	i = 2
	factors = set()
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.add(i)
	if n > 1:
		factors.add(n)
	if len(factors) == 2:
		return True
	else:
		return False

def isPrime(n):
	if n == 2 or n == 3:
		return True
	if n % 2 == 0 or n < 2:
		return False
	for i in range(3, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			return False
	return True

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
a = get_array()
if n < 3:
	print(0)
	exit()
r = []
for x in [-1, 0, 1]:
	for y in [-1, 0, 1]:
		f = 1
		t = 0
		s = a[0] + x
		d = a[1] + y - s
		b = range(s, s + n * d, d) if d else [s] * n
		for i in range(n):
			p = abs(b[i] - a[i])
			if p > 1:
				f = 0
				break
			if p == 1:
				t += 1
		if f:
			r.append(t)
print(min(r) if r else -1)


N = int(input())
a = list(map(int, input().split()))
pattern = [-1, 0, 1]
if len(a) == 1 or len(a) == 2:
	print(0)
else:
	ans = []
	for i in pattern:
		for j in pattern:
			tmp = abs(i) + abs(j)
			tolerance = a[1] + j - a[0] - i
			for k in range(2, N):
				if a[k] == a[0] + i + tolerance * k:
					continue
				elif abs(a[k] - a[0] - i - tolerance * k) == 1:
					tmp += 1
				else:
					tmp = -1
					break
			if tmp != -1:
				ans.append(tmp)
	if not ans:
		print(-1)
	else:
		print(min(ans))


n = int(input())
lis = list(map(int, input().split()))
if n < 3:
	print(0)
else:
	an = lis[-1]
	a = lis[0]
	ds = (lis[-1] - lis[0]) // (n - 1)
	chk = [ds, ds - 1, ds + 1]
	ass = [lis[0], lis[0] - 1, lis[0] + 1]
	ans = []
	for j in range(3):
		for k in range(3):
			mark = True
			i = 0
			dif = []
			while i < n:
				if abs(lis[i] - ass[j] - i * chk[k]) < 2:
					dif.append(abs(lis[i] - ass[j] - i * chk[k]))
				else:
					mark = False
					break
				i += 1
			if mark:
				ans.append(dif)
	say = 1000000
	for i in ans:
		count = 0
		for j in i:
			if j == 1:
				count += 1
		say = min(say, count)
	if say == 1000000:
		print(-1)
	else:
		print(say)


from sys import stdin, stdout
from sys import maxsize

def solve(a, n):
	d = (a[-1] - a[0]) // (n - 1)
	m = set({-1, 0, 1})
	count = 0
	for i in range(n - 2):
		if a[i] + d - a[i + 1] in m:
			if a[i] + d - a[i + 1] == -1:
				count += 1
				a[i + 1] -= 1
			if a[i] + d - a[i + 1] == 1:
				count += 1
				a[i + 1] += 1
		else:
			return maxsize
	if a[n - 2] + d - a[n - 1] != 0:
		return maxsize
	return count
test = 1
for t in range(0, test):
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n == 1 or n == 2:
		print(0)
		break
	m1 = solve(arr[:], n)
	arr[-1] += 1
	m2 = solve(arr[:], n) + 1
	arr[-1] -= 2
	m3 = solve(arr[:], n) + 1
	arr[-1] += 1
	arr[0] += 1
	m4 = solve(arr[:], n) + 1
	arr[-1] += 1
	m5 = solve(arr[:], n) + 2
	arr[-1] -= 2
	m6 = solve(arr[:], n) + 2
	arr[-1] += 1
	arr[0] -= 1
	arr[0] += -1
	m7 = solve(arr[:], n) + 1
	arr[-1] += 1
	m8 = solve(arr[:], n) + 2
	arr[-1] -= 2
	m9 = solve(arr[:], n) + 2
	ans = min(m1, m2, m3, m4, m5, m6, m7, m8, m9)
	if ans == maxsize:
		print(-1)
	else:
		print(ans)


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)


n = int(input())
arr = [int(i) for i in input().split()]
if n < 3:
	print(0)
	exit()
ans = []
for i in range(-1, 2):
	for j in range(-1, 2):
		a = arr[0] + i
		b = arr[1] + j
		changes = abs(i) + abs(j)
		diff = b - a
		curr = b + diff
		for k in range(2, n):
			add = abs(arr[k] - curr)
			curr += diff
			if add < 2:
				changes += add
			else:
				changes = n + 1
				break
		ans.append(changes)
ans = min(ans)
if ans == n + 1:
	print(-1)
else:
	print(ans)


from math import sqrt, gcd, ceil, floor, log, factorial
from itertools import permutations, combinations
from collections import Counter, defaultdict
n = int(input())
b = list(map(int, input().split()))
res = []
if n <= 2:
	print(0)
else:
	(cnt1, cnt2, cnt3, cnt4, cnt5, cnt6, cnt7, cnt8, cnt9) = (0, 1, 1, 1, 2, 2, 1, 2, 2)
	(flag1, flag2, flag3, flag4, flag5, flag6, flag7, flag8, flag9) = (0, 0, 0, 0, 0, 0, 0, 0, 0)
	a1 = b[0]
	d1 = b[1] - a1
	for i in range(2, n):
		if abs(a1 + i * d1 - b[i]) == 0:
			continue
		elif abs(a1 + i * d1 - b[i]) == 1:
			cnt1 += 1
		else:
			flag1 = 1
			break
	if flag1 == 0:
		res.append(cnt1)
	a2 = b[0]
	d2 = b[1] + 1 - a2
	for i in range(2, n):
		if abs(a2 + i * d2 - b[i]) == 0:
			continue
		elif abs(a2 + i * d2 - b[i]) == 1:
			cnt2 += 1
		else:
			flag2 = 1
			break
	if flag2 == 0:
		res.append(cnt2)
	a3 = b[0]
	d3 = b[1] - 1 - a3
	for i in range(2, n):
		if abs(a3 + i * d3 - b[i]) == 0:
			continue
		elif abs(a3 + i * d3 - b[i]) == 1:
			cnt3 += 1
		else:
			flag3 = 1
			break
	if flag3 == 0:
		res.append(cnt3)
	a4 = b[0] + 1
	d4 = b[1] - a4
	for i in range(2, n):
		if abs(a4 + i * d4 - b[i]) == 0:
			continue
		elif abs(a4 + i * d4 - b[i]) == 1:
			cnt4 += 1
		else:
			flag4 = 1
			break
	if flag4 == 0:
		res.append(cnt4)
	a5 = b[0] + 1
	d5 = b[1] + 1 - a5
	for i in range(2, n):
		if abs(a5 + i * d5 - b[i]) == 0:
			continue
		elif abs(a5 + i * d5 - b[i]) == 1:
			cnt5 += 1
		else:
			flag5 = 1
			break
	if flag5 == 0:
		res.append(cnt5)
	a6 = b[0] + 1
	d6 = b[1] - 1 - a6
	for i in range(2, n):
		if abs(a6 + i * d6 - b[i]) == 0:
			continue
		elif abs(a6 + i * d6 - b[i]) == 1:
			cnt6 += 1
		else:
			flag6 = 1
			break
	if flag6 == 0:
		res.append(cnt6)
	a7 = b[0] - 1
	d7 = b[1] - a7
	for i in range(2, n):
		if abs(a7 + i * d7 - b[i]) == 0:
			continue
		elif abs(a7 + i * d7 - b[i]) == 1:
			cnt7 += 1
		else:
			flag7 = 1
			break
	if flag7 == 0:
		res.append(cnt7)
	a8 = b[0] - 1
	d8 = b[1] + 1 - a8
	for i in range(2, n):
		if abs(a8 + i * d8 - b[i]) == 0:
			continue
		elif abs(a8 + i * d8 - b[i]) == 1:
			cnt8 += 1
		else:
			flag8 = 1
			break
	if flag8 == 0:
		res.append(cnt8)
	a9 = b[0] - 1
	d9 = b[1] - 1 - a9
	for i in range(2, n):
		if abs(a9 + i * d9 - b[i]) == 0:
			continue
		elif abs(a9 + i * d9 - b[i]) == 1:
			cnt9 += 1
		else:
			flag9 = 1
			break
	if flag9 == 0:
		res.append(cnt9)
	if res == []:
		print(-1)
	else:
		print(min(res))


from typing import List, Tuple

def mean(readings: List[int]) -> float:
	return sum(readings) / float(len(readings))

def variance(readings: List[int]) -> float:
	μ = mean(readings)
	diffSq = [pow(reading - μ, 2) for reading in readings]
	return sum(diffSq) / float(len(readings) - 1)

def covariance(l1: List[int], l2: List[int]) -> float:
	μ = [mean(l1), mean(l2)]
	cov = 0.0
	for i in range(0, len(l1)):
		cov += (l1[i] - μ[0]) * (l2[i] - μ[1])
	return cov / float(len(l1) - 1)

def linearReg(lx: List[int], ly: List[int]) -> Tuple[float, float]:
	m = covariance(lx, ly) / float(variance(lx))
	b = mean(ly) - m * mean(lx)
	return (b, m)
length = int(input())
ls = [int(s) for s in input().strip().split(' ')]

def compute() -> int:
	if length <= 2:
		return 0
	(b, m) = linearReg(list(range(0, length)), ls)
	diff = round(m)
	minChanges = 10000000000.0
	for start in range(ls[0] - 1, ls[0] + 2):
		changes = 0
		for i in range(0, length):
			dist = ls[i] - (start + diff * i)
			if dist == 0:
				continue
			if abs(dist) <= 1:
				changes += 1
				continue
			changes = 10000000000.0
			break
		if changes < minChanges:
			minChanges = changes
	if minChanges != 10000000000.0:
		return minChanges
	return -1
print(compute())


def main():
	N = int(input())
	B = [int(i) for i in input().split()]
	if N <= 2:
		print(0)
		return
	ans = 10 ** 6
	for x in (-1, 0, 1):
		for y in (-1, 0, 1):
			cur = abs(x) + abs(y)
			b0 = B[0] + x
			b1 = B[1] + y
			d = b1 - b0
			pre = b1
			for b in B[2:]:
				if b - pre == d:
					pre = b
					continue
				elif b - pre == d + 1:
					pre = b - 1
					cur += 1
				elif b - pre == d - 1:
					pre = b + 1
					cur += 1
				else:
					cur = 10 ** 6
					break
			ans = min(ans, cur)
	print(ans if ans != 10 ** 6 else -1)
main()


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)


import sys
import math
import collections
import heapq
import decimal
input = sys.stdin.readline
n = int(input())
l = [int(i) for i in input().split()]
if n <= 2:
	print(0)
else:
	c = 1000000
	for i in range(-1, 2):
		k1 = l[0] + i
		for j in range(-1, 2):
			k2 = l[1] + j
			d = k2 - k1
			c1 = 1
			c2 = abs(i) + abs(j)
			for k in range(2, n):
				k3 = k1 + k * d
				if abs(k3 - l[k]) > 1:
					c1 = 0
					break
				elif abs(k3 - l[k]) == 1:
					c2 += 1
			if c1 == 1:
				c = min(c, c2)
	if c == 1000000:
		print(-1)
	else:
		print(c)


import sys
import operator as op
next(sys.stdin)
xs = list(map(int, next(sys.stdin).rstrip().split()))
if len(xs) <= 2:
	print(0)
	sys.exit(0)
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)
		sys.exit()
print(min(frontier, key=op.itemgetter(0))[0])


n = int(input())
A = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
a = A[0]
b = A[1]
res = []
for i in range(-1, 2, 1):
	for j in range(-1, 2, 1):
		res.append([a + i, b + j])
ans = 2 * n
for j in res:
	u = j[0]
	v = j[1]
	d = v - u
	cnt = 0
	L = [u + i * d for i in range(n)]
	for i in range(n):
		if L[i] != A[i] and abs(L[i] - A[i]) == 1:
			cnt += 1
		elif L[i] == A[i]:
			continue
		else:
			cnt = -1
			break
	if cnt != -1:
		ans = min(ans, cnt)
if ans == 2 * n:
	print(-1)
else:
	print(ans)


n = int(input())
a = [int(x) for x in input().split()]
if len(a) < 3:
	print(0)
	exit()
INF = 10000000000.0
ans = INF
for a1 in range(a[0] - 1, a[0] + 2):
	for a2 in range(a[1] - 1, a[1] + 2):
		res = abs(a1 - a[0]) + abs(a2 - a[1])
		d = a2 - a1
		cur = a2
		ok = 1
		for x in a[2:]:
			cur += d
			change = abs(x - cur)
			ok &= change < 2
			res += change
		if ok:
			ans = min(ans, res)
print(ans if ans < INF else -1)


n = int(input())
array = input().split(' ')
array = [int(x) for x in array]
if n == 1 or n == 2:
	print(0)
else:
	matrix = [[0 for x in range(3)] for y in range(5)]
	for i in range(0, 5):
		k = array[1] - array[0] + i - 2
		for m in range(0, 3):
			newArray = [int(x) for x in array]
			newArray[0] = newArray[0] + m - 1
			isResult = True
			numberOfChange = 0
			if m - 1 != 0:
				numberOfChange += 1
			for y in range(1, n):
				newArray[y] = newArray[y - 1] + k
				if abs(newArray[y] - array[y]) > 1:
					isResult = False
					break
				if abs(newArray[y] - array[y]) == 1:
					numberOfChange += 1
			if isResult:
				matrix[i][m] = numberOfChange
			else:
				matrix[i][m] = -1
	result = []
	for x in range(3):
		for y in range(5):
			if matrix[y][x] > -1:
				result.append(matrix[y][x])
	if len(result) == 0:
		print(-1)
	else:
		print(min(result))


R = lambda : map(int, input().split())
n = int(input())
A = list(R())
if n == 1 or n == 2:
	print(0)
	quit()
minn = int(1000000000.0 + 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(1000000000.0 + 1):
	print(-1)
else:
	print(minn)


n = int(input())
arr = list(map(int, input().split()))
if n <= 2:
	print(0)
	exit(0)
inc = arr[0] <= arr[1]
res = 99999999999999
for first in range(arr[0] - 1, arr[0] + 2):
	for second in range(arr[1] - 1, arr[1] + 2):
		r = second - first
		isValid = True
		last = second
		cnt = 0
		if arr[0] != first:
			cnt += 1
		if arr[1] != second:
			cnt += 1
		for i in range(2, n):
			if arr[i] - 1 <= last + r <= arr[i] + 1:
				last += r
				if arr[i] != last:
					cnt += 1
				continue
			isValid = False
			break
		if isValid:
			res = min(res, cnt)
if res != 99999999999999:
	print(res)
else:
	print(-1)


from typing import List, Tuple
NOPE = 9999999999999999
length = int(input())
ls = [int(s) for s in input().strip().split(' ')]

def compute() -> int:
	if length <= 2:
		return 0
	diffSum = 0
	for i in range(1, length):
		diffSum += ls[i] - ls[i - 1]
	diff = round(diffSum / (length - 1))
	minChanges = NOPE
	for start in range(ls[0] - 1, ls[0] + 2):
		for d in range(diff - 1, diff + 2):
			changes = 0
			for i in range(0, length):
				dist = abs(ls[i] - (start + d * i))
				if dist == 0:
					continue
				if abs(dist) <= 1:
					changes += 1
					continue
				changes = NOPE
				break
			if changes < minChanges:
				minChanges = changes
	if minChanges != NOPE:
		return minChanges
	return -1
print(compute())


n = int(input())
a = [int(i) for i in input().split()]
if n > 2:
	x = a[0]
	y = a[1]
	count1 = 0
	flag1 = 0
	d = a[1] - a[0]
	a[0] = x
	a[1] = y
	c = 0
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count1 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count1 += 1
		else:
			c = 0
		if i == n - 1:
			flag1 = 1
	count2 = 2
	flag2 = 0
	a[1] = y
	a[0] = x
	d = a[1] - a[0]
	a[0] = x + 1
	a[1] = y + 1
	c = 0
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count2 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count2 += 1
		else:
			c = 0
		if i == n - 1:
			flag2 = 1
	flag3 = 0
	count3 = 2
	a[1] = y
	a[0] = x
	d = a[1] - a[0]
	a[0] = x - 1
	a[1] = y - 1
	c = 0
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count3 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count3 += 1
		else:
			c = 0
		if i == n - 1:
			flag3 = 1
	flag4 = 0
	count4 = 1
	a[1] = y
	a[0] = x
	d = a[1] + 1 - a[0]
	a[1] = y + 1
	c = 0
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count4 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count4 += 1
		else:
			c = 0
		if i == n - 1:
			flag4 = 1
	flag5 = 0
	count5 = 1
	c = 0
	a[1] = y
	a[0] = x
	d = 1 + a[1] - a[0]
	a[0] = x - 1
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count5 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count5 += 1
		else:
			c = 0
		if i == n - 1:
			flag5 = 1
	flag6 = 0
	count6 = 1
	a[1] = y
	a[0] = x
	d = a[1] - a[0] - 1
	a[0] = x + 1
	c = 0
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count6 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count6 += 1
		else:
			c = 0
		if i == n - 1:
			flag6 = 1
	flag7 = 0
	count7 = 1
	a[1] = y
	a[0] = x
	d = a[1] - a[0] - 1
	a[1] = y - 1
	c = 0
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count7 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count7 += 1
		else:
			c = 0
		if i == n - 1:
			flag7 = 1
	flag8 = 0
	count8 = 2
	a[1] = y
	a[0] = x
	d = a[1] - a[0] + 2
	a[1] = y + 1
	a[0] = x - 1
	c = 0
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count8 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count8 += 1
		else:
			c = 0
		if i == n - 1:
			flag8 = 1
	flag9 = 0
	count9 = 2
	a[1] = y
	a[0] = x
	d = a[1] - a[0] - 2
	c = 0
	a[1] = y - 1
	a[0] = x + 1
	for i in range(2, n):
		if abs(a[i] - a[i - 1] - d - c) > 1:
			break
		elif a[i] - a[i - 1] - d - c == 1:
			c = -1
			count9 += 1
		elif a[i] - a[i - 1] - d - c == -1:
			c = +1
			count9 += 1
		else:
			c = 0
		if i == n - 1:
			flag9 = 1
	count = []
	if flag1 == 1:
		count.append(count1)
	if flag2 == 1:
		count.append(count2)
	if flag3 == 1:
		count.append(count3)
	if flag4 == 1:
		count.append(count4)
	if flag5 == 1:
		count.append(count5)
	if flag6 == 1:
		count.append(count6)
	if flag7 == 1:
		count.append(count7)
	if flag8 == 1:
		count.append(count8)
	if flag9 == 1:
		count.append(count9)
	if count == []:
		print(-1)
	else:
		print(min(count))
if n <= 2:
	print(0)


n = int(input())
a = list(map(int, input().split()))
if n <= 2:
	print(0)
else:
	q = []
	for i in range(-1, 2):
		for j in range(-1, 2):
			b = a[:]
			count = abs(i) + abs(j)
			b[0] += i
			b[1] += j
			flag = False
			for k in range(2, n):
				if abs(b[0] + k * (b[1] - b[0]) - b[k]) == 1:
					count += 1
				elif abs(b[0] + k * (b[1] - b[0]) - b[k]) == 0:
					continue
				else:
					flag = True
					break
			if not flag:
				q.append(count)
	if len(q) != 0:
		print(min(q))
	else:
		print(-1)


n = int(input())
m = list(map(int, input().split()))
if n == 1:
	print(0)
else:
	sost = [-1, 0, 1]
	mi = int(10000000000.0)
	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(10000000000.0):
		print(-1)
	else:
		print(mi)


n = int(input())
arr = list(map(int, input().split()))
if n == 1 or n == 2:
	print(0)
else:
	ans = 10 ** 10
	for d1 in range(-1, 2):
		for d2 in range(-1, 2):
			(temp, a1, a2) = (0, arr[0] + d1, arr[1] + d2)
			for i in range(n):
				dif = a2 - a1
				term = a1 + i * dif
				if abs(term - arr[i]) <= 1:
					temp += abs(term - arr[i])
				else:
					temp = 10 ** 10
					break
			ans = min(ans, temp)
	print(-1 if ans == 10 ** 10 else ans)


n = int(input())
x = map(int, input().split())
if n < 3:
	print(0)
else:
	d = [-1, 0, 1]
	f = []
	c = []
	s = []
	r = []
	u = next(x)
	v = next(x)
	for d1 in d:
		for d2 in d:
			c.append(v + d2)
			s.append(v + d2 - u - d1)
			r.append(abs(d1) + abs(d2))
			f.append(True)
	for v in x:
		for (i, ff) in enumerate(f):
			if ff:
				c[i] += s[i]
				dd = abs(v - c[i])
				if dd > 1:
					f[i] = False
				else:
					r[i] += dd
	rr = [r[i] for (i, ff) in enumerate(f) if ff]
	if rr:
		print(min(rr))
	else:
		print(-1)


n = input()
b = list(input().split(' '))
x = [-1, 0, 1]
for i in range(len(b)):
	b[i] = int(b[i])

def judge(d, st):
	ans = 0
	for i in range(2, len(b)):
		u = 0
		for j in x:
			if st + d == b[i] + j:
				st = st + d
				if j != 0:
					ans += 1
				u = 1
				break
		if u == 0:
			return -1
	return ans
if len(b) <= 2:
	print(0)
else:
	ans = 99999999
	for i in x:
		for j in x:
			flag = judge(b[1] + i - (b[0] + j), b[1] + i)
			if flag != -1:
				if i != 0:
					flag += 1
				if j != 0:
					flag += 1
				ans = min(ans, flag)
	if ans != 99999999:
		print(ans)
	else:
		print(-1)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
n = ii1()
arr = iia()
if n <= 2:
	print(0)
else:
	res = float('inf')
	for i in [-1, 0, 1]:
		for j in [-1, 0, 1]:
			a = arr[0] + i
			b = arr[1] + j
			diff = b - a
			temp = [a, b]
			for k in range(2, len(arr)):
				if arr[k] - temp[-1] == diff:
					temp.append(arr[k])
				elif arr[k] + 1 - temp[-1] == diff:
					temp.append(arr[k] + 1)
				elif arr[k] - 1 - temp[-1] == diff:
					temp.append(arr[k] - 1)
				else:
					break
			else:
				cur = 0
				for (i, j) in zip(arr, temp):
					if i != j:
						cur += 1
				res = min(res, cur)
	if res == float('inf'):
		print(-1)
	else:
		print(res)


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def mk(a, b, n):
	r = [a, b]
	d = b - a
	while len(r) < n:
		b += d
		r.append(b)
	return r
n = mint()
b = list(mints())
if n == 1:
	print(0)
	exit(0)
r = 1000000
for i in range(-1, 2, 1):
	for j in range(-1, 2, 1):
		z = mk(b[0] + i, b[1] + j, n)
		c = 0
		for k in range(n):
			w = abs(z[k] - b[k])
			if w > 1:
				c += 1000000
			else:
				c += w
		r = min(r, c)
if r >= 1000000:
	print(-1)
else:
	print(r)


import sys
input = sys.stdin.readline

def calc(i, j):
	c[0] += i
	c[1] += j
	d = c[1] - c[0]
	res = abs(i) + abs(j)
	for i in range(2, n):
		d_ = c[i] - c[i - 1]
		if abs(d - d_) <= 1:
			c[i] += d - d_
			res += abs(d - d_)
		else:
			return 10 ** 18
	return res
n = int(input())
b = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
ans = 10 ** 18
for i in range(-1, 2):
	for j in range(-1, 2):
		c = b[:]
		ans = min(ans, calc(i, j))
if ans == 10 ** 18:
	print(-1)
else:
	print(ans)


n = int(input())
ary = list(map(int, input().split()))
if n < 3:
	print(0)
	quit(0)
res = n + 1
for t1 in range(-1, 2):
	for t2 in range(-1, 2):
		T1 = ary[0] + t1
		T2 = ary[1] + t2
		d = T2 - T1
		tmp = 2
		val = T2
		flag = 1
		num = abs(t1) + abs(t2)
		while tmp < n:
			val += d
			if abs(ary[tmp] - val) > 1:
				flag = 0
				break
			elif abs(ary[tmp] - val) == 1:
				num += 1
			tmp += 1
		if flag:
			res = min(res, num)
if res == n + 1:
	res = -1
print(res)


import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
import itertools
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
n = inp()
a = inpl()
if n == 1:
	print(0)
	quit()
sa = set()
for i in range(n - 1):
	sa.add(a[i + 1] - a[i])
sa = list(sa)
sa.sort()
ln = len(sa)
if ln > 6 or sa[-1] - sa[0] > 6:
	print(-1)
	quit()
sa = range(sa[0], sa[-1] + 1)
res = INF
for fir in [-1, 0, 1]:
	for ss in sa:
		ans = 0
		f = a[0] + fir
		for (i, x) in enumerate(a):
			now = f + i * ss
			if abs(x - now) > 1:
				break
			ans += abs(x - now)
		else:
			res = min(ans, res)
print(res if res != INF else -1)


n = int(input())
ns = [int(x) for x in input().split()]
po = [-1, 0, 1]
ans = 100001
if n <= 2:
	print(0)
else:
	for i in po:
		for j in po:
			cnt = 2
			if i == 0:
				cnt -= 1
			if j == 0:
				cnt -= 1
			d = ns[1] + j - ns[0] - i
			flg = True
			for k in range(2, n):
				should = ns[0] + i + k * d
				if abs(should - ns[k]) == 1:
					cnt += 1
					continue
				if abs(should - ns[k]) == 0:
					continue
				flg = False
				break
			if flg == False:
				continue
			ans = min(ans, cnt)
	if ans == 100001:
		print(-1)
	else:
		print(ans)


import math
n = int(input())
lst = list(map(int, input().split()))
fin_ans = int(1000000000.0)
if n <= 2:
	print(0)
else:
	for el1 in range(lst[0] - 1, lst[0] + 2):
		for el2 in range(lst[1] - 1, lst[1] + 2):
			ans = abs(el1 - lst[0]) + abs(el2 - lst[1])
			fl = True
			cur = el2
			dif = el2 - el1
			for el in lst[2:]:
				cur += dif
				if abs(cur - el) > 1:
					fl = False
					break
				ans += abs(cur - el)
			if fl:
				fin_ans = min(fin_ans, ans)
	print(fin_ans if fin_ans < int(1000000000.0) else -1)


n = int(input())
a = list(map(int, input().split()))
ans = list()
if n > 2:
	for i in [(-1, -1), (0, -1), (1, -1), (-1, 0), (0, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]:
		b = a.copy()
		b[0] += i[0]
		b[1] += i[1]
		d = b[1] - b[0]
		t = abs(i[0]) + abs(i[1])
		for j in range(2, n):
			dd = b[j] - b[j - 1]
			if dd == d + 1:
				b[j] -= 1
				t += 1
			elif dd == d - 1:
				b[j] += 1
				t += 1
			elif dd != d:
				break
			if j == n - 1:
				ans.append(t)
	if ans != []:
		print(min(ans))
	else:
		print(-1)
else:
	print(0)


import math
import sys
input = sys.stdin.readline

def solve(l, d):
	cnt = 0
	flag = 0
	for i in range(2, len(l)):
		a = l[0] + i * d
		if abs(a - l[i]) > 1:
			flag = 1
			break
		elif abs(a - l[i]) == 1:
			cnt += 1
	if flag:
		return 10 ** 18
	return cnt
n = int(input())
l = list(map(int, input().split()))
if n <= 2:
	print(0)
	exit()
ans = 10 ** 18
e1 = l[0]
e2 = l[1]
for i in range(-1, 2):
	for j in range(-1, 2):
		l[0] = l[0] + i
		l[1] = l[1] + j
		d = l[1] - l[0]
		ans = min(ans, solve(l, d) + abs(i) + abs(j))
		l[0] = e1
		l[1] = e2
if ans == 10 ** 18:
	print(-1)
else:
	print(ans)


n = int(input())
b = list(map(int, input().split()))
if n == 1 or n == 2:
	print(0)
	exit()
inf = 10 ** 12

def check(x, y):
	global b, n
	ret = 0
	if x != 0:
		ret += 1
	if y != 0:
		ret += 1
	a = [i for i in b]
	a[0] += x
	a[n - 1] += y
	if a[n - 1] < a[0]:
		a = a[::-1]
	if (a[n - 1] - a[0]) % (n - 1) != 0:
		return inf
	d = (a[n - 1] - a[0]) // (n - 1)
	for i in range(n - 1):
		if a[i + 1] - a[i] == d:
			pass
		elif a[i + 1] - a[i] == d + 1:
			a[i + 1] -= 1
			ret += 1
		elif a[i + 1] - a[i] == d - 1:
			a[i + 1] += 1
			ret += 1
		else:
			return inf
	return ret
ans = inf
for i in range(-1, 2):
	for j in range(-1, 2):
		ans = min(ans, check(i, j))
if ans == inf:
	print(-1)
else:
	print(ans)


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)


from copy import copy
n = int(input())
Az = list(map(int, input().split()))
if len(Az) == 1 or len(Az) == 2:
	print(0)
else:
	cn = float('inf')
	for s1 in [1, 0, -1]:
		for s2 in [-1, 0, 1]:
			A = copy(Az)
			fl = 1
			cnt = 0
			if s1 != 0:
				cnt += 1
			if s2 != 0:
				cnt += 1
			A[0] += s1
			A[1] += s2
			shag = A[1] - A[0]
			for y in range(2, len(A)):
				if A[y] - A[y - 1] == shag + 1:
					A[y] -= 1
					cnt += 1
				elif A[y] - A[y - 1] == shag - 1:
					A[y] += 1
					cnt += 1
				elif A[y] - A[y - 1] == shag:
					pass
				else:
					fl = 0
			if fl:
				cn = min(cn, cnt)
	if cn == float('inf'):
		print(-1)
	else:
		print(cn)


import copy
readinp = lambda : list(map(int, input().split()))
n = int(input())
a = readinp()
ans = n * n
t = [-1, 0, 1]
if n <= 2:
	print(0)
	exit()
bk = a[:]
for i in t:
	for j in t:
		a = bk[:]
		a[0] += i
		a[1] += j
		d = a[1] - a[0]
		tmp = (i != 0) + (j != 0)
		for k in range(2, n):
			ok = False
			for p in t:
				if a[k] + p - a[k - 1] == d:
					tmp += p != 0
					a[k] += p
					ok = True
			if not ok:
				break
		if ok:
			ans = min(ans, tmp)
if ans == n * n:
	print(-1)
else:
	print(ans)


n = int(input())
b = [int(i) for i in input().split()]
cont = 1110000
if n < 3:
	print('0')
else:
	for d1 in range(-1, 2):
		for d2 in range(-1, 2):
			a = []
			a += [b[0] + d1]
			a += [b[1] + d2]
			c = abs(d1) + abs(d2)
			f = 1
			for i in range(2, n):
				a += [a[0] + i * (a[1] - a[0])]
				if abs(a[i] - b[i]) > 1:
					f = 0
					break
				elif a[i] != b[i]:
					c += 1
			if f == 1:
				cont = min(cont, c)
	if cont == 1110000:
		cont = -1
	print(cont)


def read():
	return list(map(int, input().strip().split(' ')))
n = int(input())
arr = read()
if n <= 2:
	print(0)
else:
	(a, b) = (arr[0], arr[1])
	ans = 10 ** 9
	for i in range(a - 1, a + 2):
		for j in range(b - 1, b + 2):
			d = j - i
			tem = 0
			curr = i
			for elm in arr:
				if abs(curr - elm) > 1:
					tem = 10 ** 9
					break
				else:
					tem += abs(curr - elm)
				curr += d
			ans = min(ans, tem)
	if ans != 10 ** 9:
		print(ans)
	else:
		print(-1)


import sys
from itertools import accumulate
from bisect import bisect_left
from collections import Counter
TUPLES = [(0, 0), (0, 1), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 0), (-1, 1), (-1, -1)]

def read_from_stdin():
	n = int(input())
	inp = [int(i) for i in input().split()]
	return (n, inp)

def write_to_stdout(res):
	sys.stdout.write('%d\n' % res)

def get_answer(inp):
	ans_set = set()
	if len(inp) <= 2:
		return 0
	for tpl in TUPLES:
		start0 = inp[0] + tpl[0]
		start1 = inp[1] + tpl[1]
		diff = start1 - start0
		ans = abs(tpl[0]) + abs(tpl[1])
		count = 1
		for item in inp[2:]:
			count += 1
			diff_c = diff * count
			if item == start0 + diff_c or item == start0 + diff_c + 1 or item == start0 + diff_c - 1:
				if item != start0 + diff_c:
					ans += 1
			else:
				break
			if count == len(inp) - 1:
				ans_set.add(ans)
	if not ans_set:
		return -1
	else:
		return min(ans_set)

def main():
	(n, inp) = read_from_stdin()
	res = get_answer(inp)
	write_to_stdout(res)
main()


n = int(input())
nums = list(map(int, input().strip().split()))
if len(nums) <= 2:
	print(0)
else:
	res = []
	for i in [-1, 0, 1]:
		for j in [-1, 0, 1]:
			ok = True
			tmp = abs(i) + abs(j)
			a = nums[0] + i
			b = nums[1] + j
			d = b - a
			pre = b
			for k in range(2, n):
				cur = pre + d
				if abs(cur - nums[k]) > 1:
					ok = False
					break
				else:
					tmp += abs(cur - nums[k])
					pre = cur
			if ok:
				res.append(tmp)
	if len(res) == 0:
		print(-1)
	else:
		print(min(res))


n = int(input())
numbers = list(map(int, input().split(' ')))
if n == 1:
	print(0)
else:
	pos = set()
	for begin in [numbers[0] - 1, numbers[0], numbers[0] + 1]:
		for end in [numbers[-1] - 1, numbers[-1], numbers[-1] + 1]:
			if abs(end - begin) % (n - 1) == 0:
				pos.add((begin, end))
	cost = -1
	for (begin, end) in pos:
		current_cost = 0
		for (i, num) in enumerate(numbers):
			c = abs(begin + i * ((end - begin) / (n - 1)) - num)
			if c > 1:
				current_cost = -1
				break
			else:
				current_cost += c
		if current_cost != -1 and current_cost < cost or cost == -1:
			cost = current_cost
	print(int(cost))


(n, a) = (int(input()), list(map(int, input().split())))
can = [[0, 0], [0, 1], [0, -1], [1, -1], [1, 0], [1, 1], [-1, -1], [-1, 0], [-1, 1]]
if n == 1:
	print(0)
	exit(0)
ans = int(1000000000.0)
steps = [0 for i in range(n)]
for now in can:
	steps = [0 for i in range(n)]
	now_ans = (now[0] != 0) + (now[1] != 0)
	(steps[0], steps[1]) = now
	a[0] += now[0]
	a[1] += now[1]
	step = a[1] - a[0]
	for j in range(2, n):
		if a[j] - a[j - 1] - step == -1:
			a[j] += 1
			steps[j] = 1
			now_ans += 1
		elif a[j] - a[j - 1] - step == 1:
			a[j] -= 1
			steps[j] = -1
			now_ans += 1
		elif a[j] - a[j - 1] - step == 0:
			pass
		else:
			break
	else:
		ans = min(ans, now_ans)
	for i in range(n):
		a[i] -= steps[i]
print(-1 if ans == int(1000000000.0) else ans)


n = int(input())
if n <= 2:
	print(0)
	exit()
a = list(map(int, input().split()))
b = a.copy()
k1 = 0
q = b[1] - b[0]
for i in range(2, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k1 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k1 += 1
	elif b[i - 1] + q - b[i] == 0:
		pass
	else:
		k1 = n + 1
		break
b = a.copy()
k2 = 2
b[0] -= 1
b[1] -= 1
q = b[1] - b[0]
for i in range(2, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k2 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k2 += 1
	elif b[i - 1] + q - b[i] == 0:
		pass
	else:
		k2 = n + 1
		break
b = a.copy()
k3 = 2
b[0] += 1
b[1] += 1
q = b[1] - b[0]
for i in range(2, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k3 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k3 += 1
	elif b[i - 1] + q - a[i] == 0:
		pass
	else:
		k3 = n + 1
		break
b = a.copy()
k4 = 1
b[1] += 1
q = b[1] - b[0]
for i in range(2, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k4 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k4 += 1
	elif b[i - 1] + q - b[i] == 0:
		pass
	else:
		k4 = n + 1
		break
b = a.copy()
k5 = 1
b[1] -= 1
q = b[1] - b[0]
for i in range(1, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k5 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k5 += 1
	elif b[i - 1] + q - b[i] == 0:
		pass
	else:
		k5 = n + 1
		break
b = a.copy()
k6 = 1
b[0] += 1
q = b[1] - b[0]
for i in range(2, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k6 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k6 += 1
	elif b[i - 1] + q - b[i] == 0:
		pass
	else:
		k6 = n + 1
		break
b = a.copy()
k7 = 1
b[0] -= 1
q = b[1] - b[0]
for i in range(2, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k7 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k7 += 1
	elif b[i - 1] + q - b[i] == 0:
		pass
	else:
		k7 = n + 1
		break
b = a.copy()
k8 = 2
b[0] += 1
b[1] -= 1
q = b[1] - b[0]
for i in range(2, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k8 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k8 += 1
	elif b[i - 1] + q - b[i] == 0:
		pass
	else:
		k8 = n + 1
		break
b = a.copy()
k9 = 2
b[0] -= 1
b[1] += 1
q = b[1] - b[0]
for i in range(2, n):
	if b[i - 1] + q - b[i] == 1:
		b[i] += 1
		k9 += 1
	elif b[i - 1] + q - b[i] == -1:
		b[i] -= 1
		k9 += 1
	elif b[i - 1] + q - b[i] == 0:
		pass
	else:
		k9 = n + 1
		break
ans = min(k1, k2, k3, k4, k5, k6, k7, k8, k9)
if ans == n + 1:
	print(-1)
else:
	print(ans)


n = int(input())
a = input().split()
inp = []
ou = []
for i in range(len(a)):
	inp.append(int(a[i]))
ans = 0
if len(a) == 2 or len(a) == 1:
	print('0')
	ans = 2
else:
	dif = int(a[1]) - int(a[0])
	for l in range(2, len(a)):
		if int(a[l]) - int(a[l - 1]) != dif:
			break
		elif l == len(a) - 1:
			print('0')
			ans = 2
i = -1
while i < 2 and ans != 2:
	j = -1
	while j < 2 and ans != 2:
		count = 0
		if i != 0:
			count += 1
		if j != 0:
			count += 1
		b = inp.copy()
		b[0] += i
		b[1] += j
		diff = b[1] - b[0]
		for k in range(2, len(b)):
			if abs(b[k] - b[k - 1] - diff) > 1:
				j += 1
				break
			else:
				if b[k] - b[k - 1] - diff != 0:
					b[k] = b[k - 1] + diff
					count += 1
				if k == len(b) - 1:
					ou.append(count)
					ans = 1
					j += 1
	i += 1
if ans == 0:
	print('-1')
if ans == 1:
	print(min(ou))


from collections import Counter

def main():
	n = int(input())
	if n < 3:
		print(0)
		return
	aa = list(map(int, input().split()))
	(start, s, res) = (aa[0], aa[-1] - aa[0], [])
	for step in range((s - 2) // (n - 1), (s + 2) // (n - 1) + 1):
		cnt = Counter((a - b for (a, b) in zip(aa, range(start, start + step * n, step)))) if step else Counter((a - start for a in aa))
		(mi, ma) = (min(cnt.keys()), max(cnt.keys()))
		if ma - mi < 3:
			for m in range(mi + (ma - mi) // 2, ma - (ma - mi) // 2 + 1):
				res.append(n - cnt[m])
	print(min(res, default=-1))
main()


n = int(input())
a = list(map(int, input().split()))
if n <= 2:
	print(0)
else:
	ans = -1
	for i in range(-1, 2):
		for j in range(-1, 2):
			b = []
			x = a[0] + i
			d = a[1] + j - a[0] - i
			for t in range(n):
				b.append(t * d + x)
			good = 1
			cur = 0
			for t in range(n):
				if abs(a[t] - b[t]) > 1:
					good = 0
				cur += abs(a[t] - b[t])
			if good:
				if ans == -1:
					ans = cur
				else:
					ans = min(ans, cur)
	print(ans)


def f(start, step):
	b = [start]
	for i in range(n - 1):
		b.append(b[-1] + step)
	count = 0
	for i in range(n):
		if abs(a[i] - b[i]) == 1:
			count += 1
		elif abs(a[i] - b[i]) > 1:
			return -1
	return count
n = int(input())
a = list(map(int, input().split()))

def main():
	e = 10 ** 9
	if n == 1:
		print(0)
		return
	for i in range(a[0] - 1, a[0] + 2):
		for j in range(a[1] - a[0] - 2, a[1] - a[0] + 3):
			ne = f(i, j)
			if ne != -1:
				e = min(e, ne)
	if e == 10 ** 9:
		print(-1)
	else:
		print(e)
main()


import copy
n = int(input())
Bori = list(map(int, input().split()))
if n <= 2:
	print(0)
	exit()
ans = n + 1
op = [-1, 0, 1]
for i in op:
	for j in op:
		B = copy.deepcopy(Bori)
		tmp = abs(i) + abs(j)
		b0 = B[0] + i
		b1 = B[1] + j
		diff = b1 - b0
		B[1] += j
		for b in range(2, n):
			if B[b] - B[b - 1] == diff:
				continue
			elif B[b] - B[b - 1] == diff + 1:
				tmp += 1
				B[b] -= 1
			elif B[b] - B[b - 1] == diff - 1:
				tmp += 1
				B[b] += 1
			else:
				break
		else:
			ans = min(ans, tmp)
if ans == n + 1:
	print(-1)
else:
	print(ans)


n = int(input())
lst = [int(x) for x in input().split()]
if len(lst) <= 2:
	print(0)
else:
	lst0 = []
	for a in range(-1, 2):
		for b in range(-1, 2):
			q = lst[1] + b - (lst[0] + a)
			x = abs(a) + abs(b)
			for i in range(2, n):
				if abs(lst[i] - (lst[0] + a) - q * i) == 1:
					x += 1
				elif abs(lst[i] - (lst[0] + a) - q * i) > 1:
					x = 999999
					break
			lst0.append(x)
	if min(lst0) == 999999:
		print(-1)
	else:
		print(min(lst0))


import sys
INF = 1234567890

def solve(io):
	N = io.readInt()
	B = io.readIntArray(N)
	if N == 1:
		io.println(0)
		return
	lo = (B[-1] - B[0] - 2) // (N - 1)
	hi = (B[-1] - B[0] + 2) // (N - 1)
	b0 = B[0]
	best = INF
	for s in range(b0 - 1, b0 + 2):
		B[0] = s
		for d in range(lo, hi + 1):
			best = min(best, abs(b0 - s) + req(B, d))
	if best == INF:
		io.println(-1)
	else:
		io.println(best)

def req(B, d):
	cnt = 0
	want = B[0]
	for i in range(1, len(B)):
		want += d
		if want != B[i]:
			if abs(want - B[i]) == 1:
				cnt += 1
			else:
				return INF
	return cnt

class IO:
	input = None
	output = None
	raw = ''
	buf = []
	pos = 0

	def __init__(self, inputStream, outputStream):
		self.input = inputStream
		self.output = outputStream

	def readToBuffer(self):
		self.raw = self.input.readline().rstrip('\n')
		self.buf = self.raw.split()
		self.pos = 0

	def readString(self):
		while self.pos == len(self.buf):
			self.readToBuffer()
		ans = self.buf[self.pos]
		self.pos += 1
		return ans

	def readInt(self):
		return int(self.readString())

	def readFloat(self):
		return float(self.readString())

	def readStringArray(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.readString())
		return arr

	def readIntArray(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.readInt())
		return arr

	def readFloatArray(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.readFloat())
		return arr

	def readLine(self):
		while self.pos == len(self.buf):
			self.readToBuffer()
		if self.pos > 0:
			raise ValueError('Cannot call readline in the middle of a line.')
		return self.raw

	def print(self, s):
		self.output.write(str(s))

	def println(self, s):
		self.print(s)
		self.print('\n')

	def printlnArray(self, arr, sep=' '):
		self.println(sep.join((str(x) for x in arr)))

	def flushOutput(self):
		self.output.flush()
pythonIO = IO(sys.stdin, sys.stdout)
solve(pythonIO)
pythonIO.flushOutput()


n = int(input())
p = input().rstrip().split(' ')
if n == 1:
	print(0)
	exit()
l = []
q = []
w = []
D = []
E = []
C = []
for i in range(0, len(p)):
	l.append(int(p[i]) + 1)
	q.append(int(p[i]) - 1)
	w.append(int(p[i]))
	if i > 1:
		S = []
		S.append(int(p[i]) + 1)
		S.append(int(p[i]) - 1)
		S.append(int(p[i]))
		D.append(S)
HH = 0
for i in range(0, 3):
	if i == 0:
		A = l[0]
	elif i == 1:
		A = q[0]
	elif i == 2:
		A = w[0]
	for j in range(0, 3):
		if j == 0:
			B = l[1]
			if A >= B:
				T = abs(A - B)
				FF = B
				V = 0
				for k in range(0, len(D)):
					if B - T in D[k]:
						B = B - T
					else:
						V = 1
						break
				if V == 0:
					G = 1
					F = 0
					X = A
					Y = FF
					HH = 1
					NN = T
					if X != int(p[0]):
						F += 1
					if Y != int(p[1]):
						F += 1
					t = NN
					inc = 0
					dec = 0
					if X >= Y:
						dec = 1
					else:
						inc = 1
					if inc == 1:
						for BBB in range(0, len(p) - 2):
							if Y + t != int(p[2 + BBB]):
								F += 1
							Y = Y + t
						C.append(F)
					else:
						for BBB in range(0, len(p) - 2):
							if Y - t != int(p[2 + BBB]):
								F += 1
							Y = Y - t
						C.append(F)
			else:
				T = abs(B - A)
				V = 0
				FF = B
				for k in range(0, len(D)):
					if B + T in D[k]:
						B = B + T
						continue
					else:
						V = 1
						break
				if V == 0:
					G = 1
					F = 0
					X = A
					Y = FF
					HH = 1
					NN = T
					if X != int(p[0]):
						F += 1
					if Y != int(p[1]):
						F += 1
					t = NN
					inc = 0
					dec = 0
					if X >= Y:
						dec = 1
					else:
						inc = 1
					if inc == 1:
						for BBB in range(0, len(p) - 2):
							if Y + t != int(p[2 + BBB]):
								F += 1
							Y = Y + t
						C.append(F)
					else:
						for BBB in range(0, len(p) - 2):
							if Y - t != int(p[2 + BBB]):
								F += 1
							Y = Y - t
						C.append(F)
		elif j == 1:
			B = q[1]
			if A >= B:
				T = abs(A - B)
				V = 0
				FF = B
				for k in range(0, len(D)):
					if B - T in D[k]:
						B = B - T
						continue
					else:
						V = 1
						break
				if V == 0:
					G = 1
					F = 0
					X = A
					Y = FF
					HH = 1
					NN = T
					if X != int(p[0]):
						F += 1
					if Y != int(p[1]):
						F += 1
					t = NN
					inc = 0
					dec = 0
					if X >= Y:
						dec = 1
					else:
						inc = 1
					if inc == 1:
						for BBB in range(0, len(p) - 2):
							if Y + t != int(p[2 + BBB]):
								F += 1
							Y = Y + t
						C.append(F)
					else:
						for BBB in range(0, len(p) - 2):
							if Y - t != int(p[2 + BBB]):
								F += 1
							Y = Y - t
						C.append(F)
			else:
				T = abs(B - A)
				V = 0
				FF = B
				for k in range(0, len(D)):
					if B + T in D[k]:
						B = B + T
						continue
					else:
						V = 1
						break
				if V == 0:
					G = 1
					F = 0
					X = A
					Y = FF
					HH = 1
					NN = T
					if X != int(p[0]):
						F += 1
					if Y != int(p[1]):
						F += 1
					t = NN
					inc = 0
					dec = 0
					if X >= Y:
						dec = 1
					else:
						inc = 1
					if inc == 1:
						for BBB in range(0, len(p) - 2):
							if Y + t != int(p[2 + BBB]):
								F += 1
							Y = Y + t
						C.append(F)
					else:
						for BBB in range(0, len(p) - 2):
							if Y - t != int(p[2 + BBB]):
								F += 1
							Y = Y - t
						C.append(F)
		elif j == 2:
			B = w[1]
			if A >= B:
				T = abs(A - B)
				V = 0
				FF = B
				for k in range(0, len(D)):
					if B - T in D[k]:
						B = B - T
						continue
					else:
						V = 1
						break
				if V == 0:
					G = 1
					F = 0
					X = A
					Y = FF
					HH = 1
					NN = T
					if X != int(p[0]):
						F += 1
					if Y != int(p[1]):
						F += 1
					t = NN
					inc = 0
					dec = 0
					if X >= Y:
						dec = 1
					else:
						inc = 1
					if inc == 1:
						for BBB in range(0, len(p) - 2):
							if Y + t != int(p[2 + BBB]):
								F += 1
							Y = Y + t
						C.append(F)
					else:
						for BBB in range(0, len(p) - 2):
							if Y - t != int(p[2 + BBB]):
								F += 1
							Y = Y - t
						C.append(F)
			else:
				T = abs(B - A)
				V = 0
				FF = B
				for k in range(0, len(D)):
					if B + T in D[k]:
						B = B + T
						continue
					else:
						V = 1
						break
				if V == 0:
					G = 1
					F = 0
					X = A
					Y = FF
					HH = 1
					NN = T
					if X != int(p[0]):
						F += 1
					if Y != int(p[1]):
						F += 1
					t = NN
					inc = 0
					dec = 0
					if X >= Y:
						dec = 1
					else:
						inc = 1
					if inc == 1:
						for BBB in range(0, len(p) - 2):
							if Y + t != int(p[2 + BBB]):
								F += 1
							Y = Y + t
						C.append(F)
					else:
						for BBB in range(0, len(p) - 2):
							if Y - t != int(p[2 + BBB]):
								F += 1
							Y = Y - t
						C.append(F)
if len(C) == 0:
	print(-1)
else:
	print(min(C))


if int(input()) < 3:
	print(0)
else:
	original = list(map(int, input().split(' ')))
	options = []
	for i in range(3):
		for j in range(3):
			fst = original[0] + i - 1
			diff = original[1] + j - 1 - fst
			curr = fst + diff
			ok = True
			changes = abs(i - 1) + abs(j - 1)
			for k in range(2, len(original)):
				curr += diff
				element = original[k]
				if element == curr + 1 or element == curr - 1:
					changes += 1
				elif element != curr:
					ok = False
					break
			if ok:
				options.append(changes)
	if len(options) == 0:
		print(-1)
	else:
		print(min(options))


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)


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, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 2:
		print(0)
		return
	(s, t) = (a[1] - a[0] - 2, a[1] - a[0] + 2)
	f = False
	for j in range(2, n):
		if int((a[j] - a[0] - 2) / j) > t or int((a[j] - a[0] + 2) / j) < s:
			f = True
			break
		(s, t) = (max(int((a[j] - a[0] - 2) / j), s), min(int((a[j] - a[0] + 2) / j), t))
	if f:
		print(-1)
		return
	ans = 10 ** 10
	temp = 0
	for i in range(-1, 2):
		first = a[0] + i
		for j in range(s, t + 1):
			temp = abs(i)
			f = False
			for k in range(1, n):
				cur = first + j * k
				if a[k] - cur <= -2 or a[k] - cur >= 2:
					f = True
					break
				if a[k] != cur:
					temp += 1
			if not f:
				ans = min(ans, temp)
	if ans == 10 ** 10:
		print(-1)
	else:
		print(ans)
main()


n = int(input())
a = list(map(int, input().split()))
if n < 3:
	print(0)
else:
	y = []
	for i in range(-1, 2):
		extra1 = 0
		if i != 0:
			extra1 = 1
		b0 = a[0] + i
		for j in range(-1, 2):
			extra2 = 0
			if j != 0:
				extra2 = 1
			b1 = a[1] + j
			d = b1 - b0
			ans = 0
			log = 0
			for x in range(2, n):
				b = b0 + x * d
				if abs(b - a[x]) > 1:
					log = 1
					break
				elif b != a[x]:
					ans += 1
			if log == 0:
				y.append(ans + extra1 + extra2)
	if len(y) == 0:
		print(-1)
	else:
		print(min(y))


def main():
	n = int(input())
	bs = list(map(int, input().split(' ')))
	if n < 3:
		print(0)
		return
	mincount = n + 1
	for c0 in [-1, 0, 1]:
		for c1 in [-1, 0, 1]:
			c_current = c1
			cnt = 0
			if c0 != 0:
				cnt += 1
			if c1 != 0:
				cnt += 1
			d = bs[1] + c1 - (bs[0] + c0)
			for (b, b_next) in zip(bs[1:], bs[2:]):
				c_next = d + b - b_next + c_current
				c_current = c_next
				if c_next not in [-1, 0, 1]:
					break
				if c_next != 0:
					cnt += 1
			else:
				mincount = min(mincount, cnt)
	if mincount > n:
		print(-1)
	else:
		print(mincount)
main()


def check():
	ans = n + 1
	for d1 in range(-1, 2):
		for d2 in range(-1, 2):
			cnt = abs(d1) + abs(d2)
			a = arr[0] + d1
			diff = arr[1] + d2 - a
			done = True
			for i in range(2, n):
				toget = a + i * diff
				if abs(toget - arr[i]) > 1:
					done = False
					break
				cnt += abs(toget - arr[i])
			if done:
				ans = min(ans, cnt)
	return -1 if ans == n + 1 else ans
R = lambda : list(map(int, input().split()))
n = int(input())
arr = R()
if n <= 2:
	print(0)
else:
	print(check())


from sys import stdin

def main():
	n = int(stdin.readline())
	ar = list(map(int, stdin.readline().split()))
	if n <= 2:
		print(0)
	else:
		ans = 10 ** 18
		find = False
		for i in range(-1, 2):
			for j in range(-1, 2):
				ok = True
				curr_ans = abs(i) + abs(j)
				curr = ar[1] + j
				d = ar[1] + j - ar[0] - i
				for k in range(2, n):
					if abs(ar[k] - (curr + d)) <= 1:
						curr_ans += abs(ar[k] - (curr + d))
						curr = curr + d
					else:
						ok = False
						break
				if ok:
					ans = min(curr_ans, ans)
					find = True
		if find:
			print(ans)
		else:
			print(-1)
main()


from sys import stdin
input = stdin.buffer.readline
n = int(input())
arr = [int(x) for x in input().split()]
if n <= 2:
	print(0)
	exit()
l = []
for d1 in range(-1, 2):
	for d2 in range(-1, 2):
		a1 = arr[0] + d1
		a2 = arr[1] + d2
		d = a2 - a1
		cnt = 0
		if arr[0] != a1:
			cnt = cnt + 1
		if arr[1] != a2:
			cnt = cnt + 1
		for i in range(2, n):
			if abs(a1 + i * d - arr[i]) > 1:
				cnt = -1
				break
			if abs(a1 + i * d - arr[i]) == 1:
				cnt = cnt + 1
		if cnt != -1:
			l.append(cnt)
if len(l) == 0:
	print(-1)
else:
	print(min(l))


import sys

def pro():
	return sys.stdin.readline().strip()

def rop():
	return map(int, pro().split())

def main():
	a = int(pro())
	s = list(rop())
	if len(s) < 3:
		print(0)
		exit()
	t = 10000000000.0
	ans = t
	for i in range(s[0] - 1, s[0] + 2):
		for o in range(s[1] - 1, s[1] + 2):
			r = abs(i - s[0]) + abs(o - s[1])
			d = o - i
			q = o
			flag = 1
			for x in s[2:]:
				q += d
				w = abs(x - q)
				flag &= w < 2
				r += w
			if flag:
				ans = min(ans, r)
	if ans < t:
		print(ans)
	else:
		print(-1)
main()


import sys
import collections
input = lambda : sys.stdin.readline().rstrip('\r\n')
Read = lambda : list(map(int, input().split()))
Num = lambda : int(input())

def solveAAA(a, M):
	n = len(a)
	diff = []
	for i in range(1, n):
		diff.append(a[i] - a[i - 1])
	tot = sum(diff)
	(minn, maxx) = (tot // (n - 1), tot // (n - 1) + 1)
	(norma_, norm_a) = ([], [])
	(plusa_, plus_a) = ([], [])
	(nega_, neg_a) = ([], [])
	M = min(a)
	for i in range(n):
		norma_.append(M + i * maxx)
		norm_a.append(M + i * minn)
		plusa_.append(M + 1 + i * maxx)
		plus_a.append(M + 1 + i * minn)
		nega_.append(M - 1 + i * maxx)
		neg_a.append(M - 1 + i * minn)
	track = [1] * 6
	for i in range(n):
		if norma_[i] - a[i] > 1 or norma_[i] - a[i] < -1:
			track[0] = 0
		if norm_a[i] - a[i] > 1 or norm_a[i] - a[i] < -1:
			track[1] = 0
		if plusa_[i] - a[i] > 1 or plus_a[i] - a[i] < -1:
			track[2] = 0
		if plus_a[i] - a[i] > 1 or plus_a[i] - a[i] < -1:
			track[3] = 0
		if nega_[i] - a[i] > 1 or nega_[i] - a[i] < -1:
			track[4] = 0
		if neg_a[i] - a[i] > 1 or neg_a[i] - a[i] < -1:
			track[5] = 0
	ans = 10 ** 10
	if track == [0] * 6:
		return -1
	else:
		if track[0] == 1:
			c = 0
			for i in range(n):
				if a[i] != norma_[i]:
					c += 1
			ans = min(ans, c)
		if track[1] == 1:
			c = 0
			for i in range(n):
				if a[i] != norm_a[i]:
					c += 1
			ans = min(ans, c)
		if track[2] == 1:
			c = 0
			for i in range(n):
				if a[i] != plusa_[i]:
					c += 1
			ans = min(ans, c)
		if track[3] == 1:
			c = 0
			for i in range(n):
				if a[i] != plus_a[i]:
					c += 1
			ans = min(ans, c)
		if track[4] == 1:
			c = 0
			for i in range(n):
				if a[i] != nega_[i]:
					c += 1
			ans = min(ans, c)
		if track[5] == 1:
			c = 0
			for i in range(n):
				if a[i] != neg_a[i]:
					c += 1
			ans = min(ans, c)
		return ans

def solve():
	n = Num()
	a = Read()
	if n == 1:
		print(0)
		return
	M = min(a)
	(X, Y) = (solveAAA(a, M), solveAAA(a[::-1], M))
	if X >= 0 and Y >= 0:
		print(min(X, Y))
	elif X == -1 and Y >= 0:
		print(Y)
	elif X >= 0 and Y == -1:
		print(X)
	else:
		print(-1)
solve()


def alArth(a1, a2, a, count, n):
	dif = a2 - a1
	flag = 0
	term = 0
	for i in range(2, n):
		term = 0
		term = a1 + i * (a2 - a1)
		if abs(term - a[i]) > 1:
			flag = -1
			break
		elif term - a[i] != 0:
			count = count + 1
	if flag == -1:
		return 10 ** 9
	else:
		return count
n = int(input())
a = list(map(int, input().split()))
result = 10 ** 9
if n == 1 or n == 2:
	print(0)
else:
	c1 = alArth(a[0] + 1, a[1], a, 1, n)
	c2 = alArth(a[0] - 1, a[1], a, 1, n)
	c3 = alArth(a[0], a[1] + 1, a, 1, n)
	c4 = alArth(a[0], a[1] - 1, a, 1, n)
	c5 = alArth(a[0] + 1, a[1] + 1, a, 2, n)
	c6 = alArth(a[0] - 1, a[1] - 1, a, 2, n)
	c7 = alArth(a[0] - 1, a[1] + 1, a, 2, n)
	c8 = alArth(a[0] + 1, a[1] - 1, a, 2, n)
	c9 = alArth(a[0], a[1], a, 0, n)
	result = min(c1, result)
	result = min(c2, result)
	result = min(c3, result)
	result = min(c4, result)
	result = min(c5, result)
	result = min(c6, result)
	result = min(c7, result)
	result = min(c8, result)
	result = min(c9, result)
	if result == 10 ** 9:
		print(-1)
	else:
		print(result)


import math
import sys
import collections
import bisect
import time
import random
from itertools import permutations

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(1):
	n = int(input())
	arr = get_list()
	if n <= 2:
		print('0')
		continue
	ans = 10 ** 10
	for i in range(-1, 2):
		for j in range(-1, 2):
			temp_arr = arr.copy()
			temp_arr[0] += i
			temp_arr[1] += j
			d = temp_arr[1] - temp_arr[0]
			sol = abs(i) + abs(j)
			poss = True
			for k in range(2, n):
				if temp_arr[k] - temp_arr[k - 1] == d:
					continue
				diff = temp_arr[k] - temp_arr[k - 1]
				if abs(diff - d) <= 1:
					temp_arr[k] = temp_arr[k - 1] + d
					sol += 1
				else:
					poss = False
					break
			if poss:
				ans = min(ans, sol)
	if ans < 10 ** 10:
		print(ans)
		continue
	print(-1)


from sys import exit
n = int(input())
a = [int(x) for x in input().split()]
ans = 10 ** 10
if n <= 2:
	print(0)
	exit(0)
for i in range(a[0] - 1, a[0] + 2):
	for j in range(a[1] - 1, a[1] + 2):
		res = abs(a[0] - i) + abs(a[1] - j)
		d = j - i
		elem = j
		correct = True
		for x in a[2:]:
			elem += d
			res += abs(x - elem)
			if abs(x - elem) >= 2:
				correct = False
		if correct:
			ans = min(ans, res)
print(ans if ans < 10 ** 10 else -1)


def readInt():
	return int(input())

def readInts():
	return [int(i) for i in input().split()]
n = readInt()
a = readInts()
inf = n + 1
best = inf
if n <= 2:
	best = 0
else:
	deltas = range(-1, 2)
	for d0 in deltas:
		a0 = a[0] + d0
		for d1 in deltas:
			a1 = a[1] + d1
			can = True
			d = a1 - a0
			cnt = abs(d0) + abs(d1)
			for i in range(2, n):
				expected = a0 + d * i
				di = abs(a[i] - expected)
				if di <= 1:
					cnt += di
				else:
					can = False
					break
			if can:
				best = min(best, cnt)
print(-1 if inf == best else best)


n = int(input())
B = list(map(int, input().split()))
if n == 1 or n == 2:
	print(0)
	exit()
import copy
ans = 10 ** 18
for i in range(-1, 2):
	for j in range(-1, 2):
		cnt = abs(i) + abs(j)
		temp = copy.copy(B)
		temp[0] += i
		temp[1] += j
		d = temp[1] - temp[0]
		for k in range(2, n):
			if temp[k] == temp[k - 1] + d:
				continue
			elif temp[k] == temp[k - 1] + d - 1:
				temp[k] = temp[k - 1] + d
				cnt += 1
			elif temp[k] == temp[k - 1] + d + 1:
				temp[k] = temp[k - 1] + d
				cnt += 1
			else:
				break
		else:
			ans = min(cnt, ans)
if ans == 10 ** 18:
	print(-1)
else:
	print(ans)


n = int(input())
nums = [int(x) for x in input().split()]
a0 = [nums[0] - 1, nums[0], nums[0] + 1]
if n >= 2:
	a1 = [nums[1] - 1, nums[1], nums[1] + 1]
if n <= 2:
	print(0)
else:
	ans = 2147483647
	for a in a0:
		for aa1 in a1:
			d = aa1 - a
			temp = 0
			cnt = 0
			fail = False
			for i in range(n):
				if i == 0:
					temp += a
				else:
					temp += d
				if not temp == nums[i]:
					if abs(temp - nums[i]) > 1:
						fail = True
						break
					cnt += 1
			if not fail:
				ans = min(ans, cnt)
	if ans == 2147483647:
		print(-1)
	else:
		print(ans)


n = int(input())
l = [int(i) for i in input().split()]
if n <= 2:
	print(0)
	exit()
mini = 10 ** 20
for ind in range(-1, 2):
	for j in range(-1, 2):
		diff = 0
		a = l[0] + ind
		b = l[1] + j
		d = b - a
		c = b
		ans = [a, b]
		diff += abs(ind) + abs(j)
		for i in range(2, n):
			c += d
			ans.append(c)
			if abs(c - l[i]) > 1:
				diff += 10 ** 25
			else:
				diff += abs(c - l[i])
		mini = min(mini, diff)
print(mini if mini != 10 ** 20 else -1)


n = int(input())
l = list(map(int, input().split()))
if len(l) < 3:
	print('0')
	exit()
big = 10000000000.0
ans = big
for i in range(-1, 2):
	for j in range(-1, 2):
		a = 1
		totalchange = abs(i) + abs(j)
		d = l[1] + j - l[0] - i
		pre = l[1] + j
		for z in l[2:]:
			pre = pre + d
			change = abs(z - pre)
			a = a & (change < 2)
			totalchange = totalchange + change
		if a:
			ans = min(totalchange, ans)
print(ans if ans < big else '-1')


import math
n = int(input())
a = list(map(int, input().split()))
if n == 1 or n == 2:
	print(0)
else:
	arr = [0] * n
	ans = n + 1
	for i in range(-1, 2, 1):
		arr[0] = a[0] + i
		for j in range(-1, 2, 1):
			count = 0
			arr[1] = a[1] + j
			diff = arr[1] - arr[0]
			count += abs(j)
			count += abs(i)
			flag = 0
			for k in range(2, n):
				arr[k] = arr[0] + k * diff
				if abs(arr[k] - a[k]) > 1:
					flag = -1
					break
				elif arr[k] != a[k]:
					count += 1
			if flag == 0:
				ans = min(ans, count)
	if ans == n + 1:
		print(-1)
	else:
		print(ans)


import collections

def solve(n, arr):
	n = len(arr)
	if n <= 2:
		return 0
	INF = float('inf')
	minCount = INF
	for d1 in [-1, 0, 1]:
		for d2 in [-1, 0, 1]:
			a1 = arr[0] + d1
			a2 = arr[1] + d2
			d = a2 - a1
			count = abs(d1) + abs(d2)
			isPossible = True
			for i in range(2, n):
				ai = a1 + i * d
				if abs(ai - arr[i]) > 1:
					isPossible = False
				count += abs(ai - arr[i])
			if isPossible:
				minCount = min(minCount, count)
	return -1 if minCount == INF else minCount
n = int(input().strip())
arr = list(map(int, input().strip().split()))
print(solve(n, arr))


n = int(input())
l = list(map(int, input().split()))
an = []
if n == 1:
	print(0)
	exit()
for i in range(-1, 2):
	for k in range(-1, 2):
		spis = l.copy()
		spis[1] -= i
		spis[0] -= k
		nim = l[1] - i - (l[0] - k)
		mb = True
		ans = 0
		for z in range(2, n):
			if mb:
				if spis[z] - spis[z - 1] == nim:
					continue
				if spis[z] - spis[z - 1] == nim + 1:
					spis[z] -= 1
					ans += 1
					continue
				if spis[z] - spis[z - 1] == nim - 1:
					spis[z] += 1
					ans += 1
					continue
			mb = False
		if mb:
			if k == 0 and i == 0:
				an.append(ans)
			elif k != 0 and i != 0:
				an.append(ans + 2)
			else:
				an.append(ans + 1)
if len(an) != 0:
	print(min(an))
else:
	print(-1)


x = int(input())
s = list(map(int, input().split()))
OK = False
res = 100000000.0
if x <= 2:
	print(0)
	exit(0)
for n in range(-1, 2):
	for k in range(-1, 2):
		z = s.copy()
		fnd = True
		z[0] += n
		z[1] += k
		diff = z[1] - z[0]
		cnt = abs(n) + abs(k)
		for a in range(1, x - 1):
			df = z[a + 1] - z[a]
			if df - diff == -1:
				cnt += 1
				z[a + 1] += 1
			elif df - diff == 1:
				cnt += 1
				z[a + 1] -= 1
			elif abs(df - diff) >= 2:
				fnd = False
				break
		if fnd:
			res = min(res, cnt)
			cnt = 0
			OK = True
if OK:
	print(res)
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
if n < 3:
	print(0)
	exit()
dp = [[[n + 1, n + 1, n + 1] for j in range(5)] for i in range(n)]
d = [a[1] - a[0], a[1] - a[0] - 2, a[1] - a[0] - 1, a[1] - a[0] + 1, a[1] - a[0] + 2]
x = [-1, 1, 0]
for i in range(5):
	dp[0][i] = [1, 1, 0]
for i in range(1, n):
	for j in range(5):
		for u in range(3):
			for v in range(3):
				if dp[i - 1][j][v] != n + 1:
					if a[i] + x[u] - (a[i - 1] + x[v]) == d[j]:
						dp[i][j][u] = min(dp[i][j][u], dp[i - 1][j][v] + abs(x[u]))
o = n + 1
for j in range(5):
	for u in range(3):
		o = min(o, dp[-1][j][u])
if o > n:
	print(-1)
else:
	print(o)


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)


from math import ceil, floor

def findAP(a, d, size):
	ap = []
	for i in range(size):
		nth = a + i * d
		ap.append(nth)
	return ap
n = int(input())
b = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
tot = 0
for i in range(1, n):
	tot += b[i] - b[i - 1]
if tot < 0:
	cd = floor(tot / (n - 1))
else:
	cd = ceil(tot / (n - 1))
globalArr = []
for d in (cd, cd + 1, cd - 1):
	ap1 = findAP(b[0], d, n)
	ap2 = findAP(b[0] + 1, d, n)
	ap3 = findAP(b[0] - 1, d, n)
	for each in (ap1, ap2, ap3):
		val = 0
		flag = True
		for j in range(n):
			if abs(each[j] - b[j]) <= 1:
				val += abs(each[j] - b[j])
			else:
				flag = False
				break
		if flag:
			globalArr.append(val)
if globalArr:
	print(min(globalArr))
else:
	print(-1)


def solve(n, bi):
	if n == 1:
		return 0
	prog = (bi[n - 1] - bi[0]) // (n - 1)
	min_so_far = 1000000000
	for cand in range(prog - 2, prog + 3):
		for start in range(bi[0] - 1, bi[0] + 2):
			count = 0
			failed = False
			for (index, x) in enumerate(bi):
				diff = x - (cand * index + start)
				if diff != 0:
					count = count + 1
				if abs(diff) > 1:
					failed = True
					break
			if not failed:
				min_so_far = min(min_so_far, count)
	if min_so_far != 1000000000:
		return min_so_far
	else:
		return -1
n = int(input())
bi = [int(x) for x in input().split()]
print(solve(n, bi))


n = int(input())
if n == 1:
	b = int(input())
	print(0)
else:
	arr = list(map(int, input().split()))
	a = arr[0]
	b = arr[1]
	d = b - a
	ansar = []
	flag1 = 0
	ans1 = 0
	a1 = a
	d1 = d
	for i in range(2, n):
		if abs(arr[i] - (a1 + i * d1)) == 1:
			ans1 += 1
		elif abs(arr[i] - (a1 + i * d1)) == 0:
			continue
		else:
			flag1 = 1
			break
	if flag1 == 0:
		ansar.append(ans1)
	flag2 = 0
	ans2 = 1
	a2 = a
	d2 = d - 1
	for i in range(2, n):
		if abs(arr[i] - (a2 + i * d2)) == 1:
			ans2 += 1
		elif abs(arr[i] - (a2 + i * d2)) == 0:
			continue
		else:
			flag2 = 1
			break
	if flag2 == 0:
		ansar.append(ans2)
	flag3 = 0
	ans3 = 1
	a3 = a - 1
	d3 = d + 1
	for i in range(2, n):
		if abs(arr[i] - (a3 + i * d3)) == 1:
			ans3 += 1
		elif abs(arr[i] - (a3 + i * d3)) == 0:
			continue
		else:
			flag3 = 1
			break
	if flag3 == 0:
		ansar.append(ans3)
	flag4 = 0
	ans4 = 1
	a4 = a + 1
	d4 = d - 1
	for i in range(2, n):
		if abs(arr[i] - (a4 + i * d4)) == 1:
			ans4 += 1
		elif abs(arr[i] - (a4 + i * d4)) == 0:
			continue
		else:
			flag4 = 1
			break
	if flag4 == 0:
		ansar.append(ans4)
	flag5 = 0
	ans5 = 1
	a5 = a
	d5 = d + 1
	for i in range(2, n):
		if abs(arr[i] - (a5 + i * d5)) == 1:
			ans5 += 1
		elif abs(arr[i] - (a5 + i * d5)) == 0:
			continue
		else:
			flag5 = 1
			break
	if flag5 == 0:
		ansar.append(ans5)
	flag6 = 0
	ans6 = 2
	a6 = a + 1
	d6 = d
	for i in range(2, n):
		if abs(arr[i] - (a6 + i * d6)) == 1:
			ans6 += 1
		elif abs(arr[i] - (a6 + i * d6)) == 0:
			continue
		else:
			flag6 = 1
			break
	if flag6 == 0:
		ansar.append(ans6)
	flag7 = 0
	ans7 = 2
	a7 = a - 1
	d7 = d
	for i in range(2, n):
		if abs(arr[i] - (a7 + i * d7)) == 1:
			ans7 += 1
		elif abs(arr[i] - (a7 + i * d7)) == 0:
			continue
		else:
			flag7 = 1
			break
	if flag7 == 0:
		ansar.append(ans7)
	flag8 = 0
	ans8 = 2
	a8 = a + 1
	d8 = d - 2
	for i in range(2, n):
		if abs(arr[i] - (a8 + i * d8)) == 1:
			ans8 += 1
		elif abs(arr[i] - (a8 + i * d8)) == 0:
			continue
		else:
			flag8 = 1
			break
	if flag8 == 0:
		ansar.append(ans8)
	flag9 = 0
	ans9 = 2
	a9 = a - 1
	d9 = d + 2
	for i in range(2, n):
		if abs(arr[i] - (a9 + i * d9)) == 1:
			ans9 += 1
		elif abs(arr[i] - (a9 + i * d9)) == 0:
			continue
		else:
			flag9 = 1
			break
	if flag9 == 0:
		ansar.append(ans9)
	if ansar == []:
		print(-1)
	else:
		print(min(ansar))


def solve(n: int, b: list):
	if n == 1:
		print(0)
		return
	answer = n + 1
	diff = b[1] - b[0]
	for d in range(diff - 2, diff + 3):
		for start in range(b[0] - 1, b[0] + 2):
			(pos, to_change) = (start, 0)
			for i in range(n):
				if abs(pos - b[i]) > 1:
					to_change = n + 1
					break
				if pos != b[i]:
					to_change += 1
				pos += d
			answer = min(answer, to_change)
	print(-1 if answer == n + 1 else answer)
n = int(input())
b = list(map(int, input().split()))
solve(n, b)


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)
main()


from sys import stdin, stdout
input = stdin.readline
t = 1
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n <= 2:
		print(0)
		continue
	d = a[1] - a[0]
	vals = [d - 2, d - 1, d, d + 1, d + 2]
	ans = float('inf')
	for i in vals:
		start = a[0] - 1
		temp = 0
		for j in range(n):
			if a[j] == start:
				pass
			elif abs(a[j] - start) == 1:
				temp += 1
			else:
				break
			start += i
		else:
			ans = min(ans, temp)
		start = a[0]
		temp = 0
		for j in range(n):
			if a[j] == start:
				pass
			elif abs(a[j] - start) == 1:
				temp += 1
			else:
				break
			start += i
		else:
			ans = min(ans, temp)
		start = a[0] + 1
		temp = 0
		for j in range(n):
			if a[j] == start:
				pass
			elif abs(a[j] - start) == 1:
				temp += 1
			else:
				break
			start += i
		else:
			ans = min(ans, temp)
	if ans == float('inf'):
		ans = -1
	print(ans)


from sys import stdin, stdout
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))
PI = float('inf')

def fn(a):
	diff = a[1] - a[0]
	op = 0
	for i in range(2, n):
		next = a[i - 1] + diff
		if abs(next - a[i]) <= 1:
			op += abs(next - a[i])
			a[i] = next
		else:
			return PI
	return op
for _ in range(1):
	n = nmbr()
	a1 = lst()
	if n <= 2:
		print(0)
		continue
	ans = PI
	for i in range(-1, 2):
		for j in range(-1, 2):
			a1[0] += i
			a1[1] += j
			ans = min(ans, fn(a1.copy()) + abs(i) + abs(j))
			a1[0] -= i
			a1[1] -= j
	print(ans if ans != PI else -1)


n = int(input())
seq = [int(x) for x in input().split(' ')]
newSeq = list(seq)
if len(seq) <= 2:
	print(0)
else:
	MAX = 99999999
	ans = MAX
	for i in range(-1, 2, 1):
		for j in range(-1, 2, 1):
			newSeq[0] = seq[0] + i
			newSeq[1] = seq[1] + j
			modCount = abs(i) + abs(j)
			commonDiff = newSeq[1] - newSeq[0]
			for k in range(2, len(seq), 1):
				newSeq[k] = newSeq[k - 1] + commonDiff
				diff = abs(newSeq[k] - seq[k])
				if diff > 1:
					modCount = MAX
					break
				modCount += diff
			ans = min(ans, modCount)
	if ans == MAX:
		print(-1)
	else:
		print(ans)


import os, sys
from io import BytesIO, IOBase

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
import math
for i in range(1):
	n = ii()
	a = li()
	if n <= 2:
		print(0)
		exit()
	ans = -1
	for p in range(-1, 2):
		for q in range(-1, 2):
			e = a[0] + p
			f = a[1] + q
			d = f - e
			c = 0
			if p != 0:
				c += 1
			if q != 0:
				c += 1
			for i in range(2, n):
				y = e + i * d
				if abs(y - a[i]) == 1:
					c += 1
				elif abs(y - a[i]) == 0:
					continue
				else:
					c = -1
					break
			if c >= 0:
				if ans == -1 or c < ans:
					ans = c
	print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = -1
if n < 3:
	ans = 0
else:
	for d1 in range(-1, 2):
		for d2 in range(-1, 2):
			i = (a[0] + d1, a[1] + d2, int(d1 != 0) + int(d2 != 0))
			d = i[1] - i[0]
			k = i[2]
			b = i[1]
			for j in range(2, n):
				b += d
				if a[j] == b:
					k = k
				elif abs(a[j] - b) == 1:
					k += 1
				else:
					break
				if j == n - 1:
					if k < ans or ans == -1:
						ans = k
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = -1
if n < 3:
	ans = 0
elif n > 2:
	f = [(a[0], a[1], 0), (a[0], a[1] - 1, 1), (a[0], a[1] + 1, 1), (a[0] - 1, a[1] - 1, 2), (a[0] - 1, a[1], 1), (a[0] - 1, a[1] + 1, 2), (a[0] + 1, a[1] - 1, 2), (a[0] + 1, a[1], 1), (a[0] + 1, a[1] + 1, 2)]
	mn = n + 1
	for i in f:
		d = i[1] - i[0]
		k = i[2]
		b = i[1]
		for j in range(2, n):
			if a[j] - b == d:
				k = k
				b = a[j]
			elif a[j] - 1 - b == d:
				k += 1
				b = a[j] - 1
			elif a[j] + 1 - b == d:
				k += 1
				b = a[j] + 1
			else:
				break
			if j == n - 1:
				if k < mn:
					ans = k
					mn = k
print(ans)


n = int(input())
a = list(map(int, input().split()))
if n <= 2:
	print(0)
else:
	ans = 10 ** 9 + 1
	for d1 in range(-1, 2):
		for d2 in range(-1, 2):
			count = 0
			a1 = a[0] + d1
			a2 = a[1] + d2
			d = a2 - a1
			for i in range(0, n):
				val = abs(a[i] - a1 - i * d)
				if val <= 1:
					count += val
				else:
					count = 10 ** 9 + 1
					break
			ans = min(ans, count)
	print(-1 if ans > 10 ** 9 else ans)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
I = sys.stdin.readline
alpha = 'abcdefghijklmnopqrstuvwxyz'

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

def main():
	n = ii()
	arr = li()
	if n <= 2:
		print(0)
	else:
		cnt = 0
		ans = math.inf
		for i in [-1, 0, 1]:
			for j in [-1, 0, 1]:
				tmp = 0
				a1 = arr[0] + i
				a2 = arr[1] + j
				if i != 0:
					tmp += 1
				if j != 0:
					tmp += 1
				diff = a2 - a1
				go = 1
				last = a2
				for k in range(2, n):
					if arr[k] - last + 1 == diff:
						last = arr[k] + 1
						tmp += 1
					elif arr[k] - last - 1 == diff:
						last = arr[k] - 1
						tmp += 1
					elif arr[k] - last != diff:
						go = 0
						break
					else:
						last = arr[k]
				if go == 1:
					ans = min(ans, tmp)
					cnt = 1
		if cnt:
			print(ans)
		else:
			print(-1)
main()


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
n = int(input())
l = list(map(int, input().split()))
if n == 1:
	print(0)
	sys.exit(0)
l1 = l + []
ans = n
f = 0
for i in range(-1, 2):
	for j in range(-1, 2):
		l = l1 + []
		l[0] += i
		l[-1] += j
		dif = l[0] - l[-1]
		if dif % (n - 1) != 0:
			continue
		dif //= n - 1
		cur = 0
		tr = 0
		for k in range(1, n):
			dif1 = l[k - 1] - l[k]
			if k == n - 1 and dif1 != dif:
				tr = 1
				break
			if abs(dif1 - dif) > 1:
				tr = 1
				break
			else:
				l[k] += dif1 - dif
				cur += abs(dif1 - dif)
		if tr == 0:
			f = 1
			ans = min(ans, cur + abs(i) + abs(j))
		else:
			continue
if f == 0:
	print(-1)
else:
	print(ans)


fast = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from re import *
from sys import *
from math import *
from heapq import *
from queue import *
from bisect import *
from string import *
from itertools import *
from collections import *
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from collections import Counter as cc
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def output(answer):
	stdout.write(str(answer))
n = int(z())
arr = zzz()
if n <= 2:
	exit(print(0))
ans = -1
for i in range(-1, 2):
	for j in range(-1, 2):
		cnt = []
		x = arr[0] + i
		y = arr[1] + j
		d = y - x
		for t in range(n):
			cnt.append(t * d + x)
		good = 1
		cur = 0
		for t in range(n):
			if abs(arr[t] - cnt[t]) > 1:
				good = 0
			cur += abs(arr[t] - cnt[t])
		if good:
			if ans == -1:
				ans = cur
			else:
				ans = min(ans, cur)
print(ans)


def mufun(n, b):
	if n <= 2:
		print(0)
		return
	ans = n + 1
	diff = b[1] - b[0]
	for d in range(diff - 2, diff + 3):
		for start in range(b[0] - 1, b[0] + 2):
			(pos, count) = (start, 0)
			for i in range(n):
				if abs(b[i] - pos) > 1:
					count = n + 1
					break
				if b[i] != pos:
					count += 1
				pos += d
			ans = min(count, ans)
	if ans == n + 1:
		print(-1)
		return
	print(ans)
	return
n = int(input())
b = list(map(int, input().split()))
mufun(n, b)


import sys
n = int(input())
nos = input().split(' ')
if len(nos) <= 2:
	print('0')
	sys.exit()
nos = list(map(int, nos))
anss = []
if (nos[0] - 1 - (nos[n - 1] - 1)) % (n - 1) == 0:
	ini = nos[0] - 1
	rat = (nos[0] - 1 - (nos[n - 1] - 1)) / (n - 1)
	anss.append((ini, int(rat)))
if (nos[0] - 1 - nos[n - 1]) % (n - 1) == 0:
	ini = nos[0] - 1
	rat = (nos[0] - 1 - nos[n - 1]) / (n - 1)
	anss.append((ini, int(rat)))
if (nos[0] - 1 - (nos[n - 1] + 1)) % (n - 1) == 0:
	ini = nos[0] - 1
	rat = (nos[0] - 1 - (nos[n - 1] + 1)) / (n - 1)
	anss.append((ini, int(rat)))
if (nos[0] - (nos[n - 1] - 1)) % (n - 1) == 0:
	ini = nos[0]
	rat = (nos[0] - (nos[n - 1] - 1)) / (n - 1)
	anss.append((ini, int(rat)))
if (nos[0] - nos[n - 1]) % (n - 1) == 0:
	ini = nos[0]
	rat = (nos[0] - nos[n - 1]) / (n - 1)
	anss.append((ini, int(rat)))
if (nos[0] - (nos[n - 1] + 1)) % (n - 1) == 0:
	ini = nos[0]
	rat = (nos[0] - (nos[n - 1] + 1)) / (n - 1)
	anss.append((ini, int(rat)))
if (nos[0] + 1 - (nos[n - 1] - 1)) % (n - 1) == 0:
	ini = nos[0] + 1
	rat = (nos[0] + 1 - (nos[n - 1] - 1)) / (n - 1)
	anss.append((ini, int(rat)))
if (nos[0] + 1 - nos[n - 1]) % (n - 1) == 0:
	ini = nos[0] + 1
	rat = (nos[0] + 1 - nos[n - 1]) / (n - 1)
	anss.append((ini, int(rat)))
if (nos[0] + 1 - (nos[n - 1] + 1)) % (n - 1) == 0:
	ini = nos[0] + 1
	rat = (nos[0] + 1 - (nos[n - 1] + 1)) / (n - 1)
	anss.append((ini, int(rat)))
ans = 100001
for t in anss:
	c = 0
	ini = t[0]
	rat = t[1]
	if ini != nos[0]:
		c += 1
	for i in range(1, len(nos)):
		ini = ini - rat
		if abs(nos[i] - ini) == 1:
			c += 1
		elif abs(nos[i] - ini) > 1:
			c = -2
			break
	if c >= 0 and c < ans:
		ans = c
if ans == 100001:
	print('-1')
else:
	print(ans)


n = int(input())
b = list(map(int, input().split()))
ans = 114514
if n == 1:
	print(0)
	exit()
for i in [-1, 0, 1]:
	for j in [-1, 0, 1]:
		x = b[0] + i - (b[1] + j)
		y = b[1] + j
		cnt = abs(i) + abs(j)
		ok = 1
		for k in range(2, n):
			if abs(y - b[k] - x) > 1:
				ok = 0
				break
			y = y - x
			cnt += abs(y - b[k])
		if ok:
			ans = min(ans, cnt)
print(ans if not ans == 114514 else -1)


n = int(input())
a = [int(i) for i in input().split()]
if n == 3 and a[2] == 3:
	print(2)
	exit()
if n <= 2:
	print(0)
	exit()
ans = n + 1
d1 = [0, 0, 0, -1, 1, -1, -1, 1, 1]
d2 = [0, -1, 1, 0, 0, 1, -1, 1, -1]
if a[0] <= a[n - 1]:
	for i in range(9):
		b1 = a[0]
		b2 = a[1]
		b1 += d1[i]
		b2 += d2[i]
		d = abs(b2 - b1)
		c = 0
		k = 0
		if i > 0 and i < 5:
			c += 1
		elif i >= 5:
			c += 2
		for j in range(2, n):
			v = b1 + j * d
			if abs(a[j] - v) == 1:
				c += 1
			elif abs(a[j] - v) == 0:
				c += 0
			else:
				k = 1
				break
		if c < ans and k != 1:
			ans = c
	if ans == n + 1:
		print(-1)
	else:
		print(ans)
else:
	for i in range(9):
		b1 = a[0]
		b2 = a[1]
		b1 += d1[i]
		b2 += d2[i]
		d = abs(b2 - b1)
		c = 0
		k = 0
		if i > 0 and i < 5:
			c += 1
		elif i >= 5:
			c += 2
		for j in range(2, n):
			v = b1 - j * d
			if abs(a[j] - v) == 1:
				c += 1
			elif abs(a[j] - v) == 0:
				c += 0
			else:
				k = 1
				break
		if c < ans and k != 1:
			ans = c
	if ans == n + 1:
		print(-1)
	else:
		print(ans)


n = int(input())
b = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
mini = float('inf')
for x in (0, 1, -1):
	for y in (0, 1, -1):
		a = b.copy()
		a[0] += x
		a[1] += y
		c = abs(x) + abs(y)
		for i in range(2, n):
			if abs(a[1] - a[0] - (a[i] - a[i - 1])) > 1:
				break
			c += abs(a[1] - a[0] - (a[i] - a[i - 1]))
			a[i] += a[1] - a[0] - (a[i] - a[i - 1])
		else:
			mini = min(mini, c)
if mini == float('inf'):
	print(-1)
else:
	print(mini)


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)


def fun(n, a):
	if n <= 2:
		return 0
	moves = [(1, 1), (1, 0), (1, -1), (0, 1), (0, 0), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
	result = -1
	for move in moves:
		curr_result = abs(move[0]) + abs(move[1])
		prev = a[1] + move[1]
		(diff, possible) = (prev - (a[0] + move[0]), True)
		for elem in a[2:]:
			curr_diff = elem - prev - diff
			if abs(curr_diff) <= 1:
				curr_result += abs(curr_diff)
				prev = elem - curr_diff
			else:
				possible = False
				break
		if possible:
			if result == -1:
				result = curr_result
			result = min(result, curr_result)
	return result
n = int(input())
a = [int(x) for x in input().split(' ')]
print(fun(n, a))