CodeEff/ECCO huggingface dataset:
# Original solution
N = int(eval(input()))
src = [list(map(int,input().split())) for i in range(N)]

ans = 0
for a,b in reversed(src):
    r = (ans+a) % b
    if r == 0: continue
    ans += (b-r)
print(ans)


# Optimized solution
N = int(eval(input()))
src = [tuple(map(int,input().split())) for i in range(N)]

ans = 0
for a,b in reversed(src):
    a += ans
    ans += (b-a)%b
print(ans)
# Original solution
N = int(eval(input()))
src = [tuple(map(int,input().split())) for i in range(N)]

ans = 0
for a,b in src[::-1]:
    ans += (-(a+ans))%b
print(ans)

# Optimized solution
N = int(eval(input()))
AB = [tuple(map(int,input().split())) for i in range(N)]
z = 0
for a,b in AB[::-1]:
    z += -(z+a)%b
print(z)
# Original solution
n = int(eval(input()))
a = []
b = []
for i in range(n):
    A, B = list(map(int, input().split()))
    a.append(A)
    b.append(B)
ans = 0
a = a[::-1]
b = b[::-1]
for i in range(n):
    if a[i] % b[i] == 0:
        continue
    p = (a[i] // b[i] + 1) * b[i] - a[i]
    ans += p
    for j in range(i, n):
        a[j] += p
print(ans)

# Optimized solution
n = int(eval(input()))
a = []
b = []
for i in range(n):
    A, B = list(map(int, input().split()))
    a.append(A)
    b.append(B)
ans = 0
a = a[::-1]
b = b[::-1]
for i in range(n):
    a[i] += ans
    if a[i] % b[i] == 0:
        continue
    ans += (a[i] // b[i] + 1) * b[i] - a[i]
print(ans)
# Original solution
n = int(eval(input()))
a = []
b = []
for i in range(n):
    A, B = list(map(int, input().split()))
    a.append(A)
    b.append(B)
ans = 0
a = a[::-1]
b = b[::-1]
for i in range(n):
    a[i] += ans
    if a[i] % b[i] == 0:
        continue
    ans += (a[i] // b[i] + 1) * b[i] - a[i]
print(ans)

# Optimized solution
n = int(eval(input()))
a = []
b = []
for i in range(n):
    A, B = list(map(int, input().split()))
    a.append(A)
    b.append(B)
a.reverse()
b.reverse()
ans = 0
for i in range(n):
    if (a[i] + ans) % b[i]:
        ans += b[i] - (a[i] + ans) % b[i]
print(ans)
# Original solution
N = int(eval(input()))
ablist = []
for _ in range(N):
    ab = list(map(int, input().split()))
    ablist.append(ab)
ans = 0
for a, b in ablist[::-1]:
    a += ans
    if a % b == 0:
        continue
    else:
        ans += (b - a % b)
print(ans)


# Optimized solution
N = int(eval(input()))
ABlist = [tuple(map(int, input().split())) for _ in range(N)]
ans = 0
for a, b in ABlist[::-1]:
    a += ans
    if a % b == 0:
        continue
    else:
        ans += (b - a % b)
print(ans)
# Original solution
n=int(eval(input()))
A,B=[],[]
for _ in range(n):
    a,b=list(map(int,input().split()))
    A.append(a)
    B.append(b)
ans=0
for i in range(n-1,-1,-1):
    a,b=A[i],B[i]
    a+=ans
    ans+=b-a%b if a%b else 0
print(ans)

# Optimized solution
n=int(eval(input()))
A,B=[],[]
for _ in range(n):
    a,b=list(map(int,input().split()))
    A.append(a)
    B.append(b)
ans=0
for a,b in zip(A[::-1],B[::-1]):
    a+=ans
    ans+=b-a%b if a%b else 0
print(ans)
# Original solution
n = int(eval(input()))
ab = [list(map(int, input().split())) for _ in range(n)]

ans = 0
for a, b in reversed(ab):
    r = (ans+a) % b
    if r != 0:
        ans += b - r

print(ans)

# Optimized solution
n = int(eval(input()))
ab = [list(map(int, input().split())) for _ in range(n)]

ans = 0
for a, b in reversed(ab):
    a += ans
    ans += (-a) % b

print(ans)
# Original solution
n = int(eval(input()))
ab = [list(map(int, input().split())) for _ in range(n)]

ans = 0
for a, b in reversed(ab):
    while (a+ans) % b != 0:
        ans += 1

print(ans)

# Optimized solution
n = int(eval(input()))
ab = [list(map(int, input().split())) for _ in range(n)]

ans = 0
for a, b in reversed(ab):
    if (a + ans) % b != 0:
        ans += b - (a + ans) % b

print(ans)
# Original solution
N, *AB = list(map(int, open(0).read().split()))
ans = 0
for i in range(N):
    index = N - 1 - i
    a = AB[index * 2 + 0] + ans
    b = AB[index * 2 + 1]
    remainder = a % b
    ans += (b - remainder) % b
print(ans)


# Optimized solution
def main():
    N, *AB = list(map(int, open(0).read().split()))
    ans = 0
    for i in range(N):
        index = N - 1 - i
        a = AB[index * 2 + 0] + ans
        b = AB[index * 2 + 1]
        remainder = a % b
        ans += (b - remainder) % b
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
def main():
    N, *AB = list(map(int, open(0).read().split()))
    ans = 0
    for i in range(N):
        index = N - 1 - i
        a = AB[index * 2 + 0] + ans
        b = AB[index * 2 + 1]
        remainder = a % b
        ans += (b - remainder) % b
    print(ans)

if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N, *AB = list(map(int, open(0).read().split()))
    ans = 0
    for i in range(N - 1, -1, -1):
        a = AB[i * 2 + 0]
        b = AB[i * 2 + 1]
        ans += (b - a - ans) % b
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
a = int(eval(input()))
ar = []
for i in range(a):
    l = list(map(int,input().split(" ")))
    ar.insert(0,l)
count = 0
for r in ar:
    x = r[0] + count
    y = r[1]
    m = x % y
    if m != 0:
        count += y - m
print(count)

# Optimized solution
a = int(eval(input()))
ar = []
for i in range(a):
    l = list(map(int,input().split(" ")))
    ar.append(l)
ar.reverse()
count = 0
for r in ar:
    x = r[0] + count
    y = r[1]
    m = x % y
    if m != 0:
        count += y - m
print(count)
# Original solution
n = int(eval(input()))
ABs = [list(map(int, input().split())) for i in range(n)]
ABs = [i for i in reversed(ABs)]

As,Bs = [],[]
for i in range(n):
    As.append(ABs[i][0])
    Bs.append(ABs[i][1])

cnt = 0
for i in range(n):
    plus = 0 if As[i] % Bs[i] == 0 else Bs[i] - As[i] % Bs[i]
    As[i:] = list([x + plus for x in As[i:]])
    cnt += plus
    
print(cnt)

# Optimized solution
n = int(eval(input()))
ABs = [list(map(int, input().split())) for i in range(n)]
cnt = 0
for i in range(n):
    plus = 0 if (ABs[n-i-1][0] + cnt) % ABs[n-i-1][1] == 0 else ABs[n-i-1][1] - (ABs[n-i-1][0] + cnt) % ABs[n-i-1][1]
    cnt += plus
print(cnt)
# Original solution
n = int(eval(input()))
ans = 0

ab = [tuple(map(int,input().split())) for i in range(n)]
a,b = [],[]
for ai,bi in ab:
    a.append(ai)
    b.append(bi)

c = 0
for i in reversed(list(range(n))):
    a[i] += c
    while(a[i]%b[i]):
        c+=1
        a[i]+=1
    
print(c)

# Optimized solution
n = int(eval(input()))
ans = 0

a, b = [], []
for i in range(n):
    ai, bi = list(map(int, input().split()))

    a.append(ai)
    b.append(bi)

c = 0
for i in reversed(list(range(n))):
    a[i] += c
    x = a[i] % b[i]
    if x:
        if a[i] < b[i]:
            c += b[i] - a[i]
        elif a[i] > b[i]:
            c += b[i] - x

print(c)
# Original solution
N = int(eval(input()))
import math

A = []
B = []
for i in range(N):
  a, b = list(map(int,input().split()))
  A.append(int(a))
  B.append(int(b))

count=0
for i in range(N):
  #print(i,A[N-1-i],B[N-1-i])
  if A[N-1-i]%B[N-1-i]==0:
    n=0
  elif A[N-1-i]>B[N-1-i]:
    n=math.ceil(A[N-1-i]/B[N-1-i])*B[N-1-i]-A[N-1-i]
  elif A[N-1-i]<B[N-1-i]:
    n= B[N-1-i]-A[N-1-i]
  count +=n
  #print(n)
  for j in range(N-i):
    A[j]+=n
  #print(A) 
  #print(B) 
print(count)

# Optimized solution
N = int(eval(input()))
import math

A = []
B = []
for i in range(N):
  a, b = list(map(int,input().split()))
  A.append(int(a))
  B.append(int(b))

n=0
for i in range(N):
  #print(i,A[N-1-i]+n,B[N-1-i]+n)
  
  if (A[N-1-i]+n)%(B[N-1-i])==0:
    n+=0
  elif A[N-1-i]+n>B[N-1-i]:
    n+=math.ceil((A[N-1-i]+n)/B[N-1-i])*(B[N-1-i])-A[N-1-i]-n
    #print('!',n,math.ceil((A[N-1-i]+n)/(B[N-1-i])))
  elif A[N-1-i]+n<B[N-1-i]:
    n+= B[N-1-i]-A[N-1-i]-n
  #print(n)  
    
  #count +=n
  #print(n)
  #for j in range(N-i):
    #A[j]+=n

print(n)
# Original solution
# https://atcoder.jp/contests/agc009/tasks/agc009_a

n=int(eval(input()))
ab=[tuple(map(int,input().split())) for _ in range(n)]

ans=0
for a,b in reversed(ab):
    a+=ans
    res=a%b
    if res:
        ans+=b-res
print(ans)

# Optimized solution
# https://atcoder.jp/contests/agc009/tasks/agc009_a

import sys
input=sys.stdin.readline

n=int(eval(input()))
ab=[tuple(map(int,input().split())) for _ in range(n)]

ans=0
for a,b in reversed(ab):
    a+=ans
    res=a%b
    if res:
        ans+=b-res
print(ans)
# Original solution
N = int(eval(input()))
A = [0]*N
B = [0]*N
for i in range(N):
    a, b = list(map(int, input().split()))
    A[i] = a
    B[i] = b
ans = 0
for i in range(N-1,-1,-1):
    A[i] += ans
    ans += B[i]*((A[i]+B[i]-1)//B[i]) - A[i]
print(ans)


# Optimized solution
import sys
input = sys.stdin.readline

N = int(eval(input()))
A = [0]*N
B = [0]*N
for i in range(N):
    a, b = list(map(int, input().split()))
    A[i] = a
    B[i] = b
ans = 0
for i in range(N-1,-1,-1):
    A[i] += ans
    if A[i]%B[i] != 0:
        ans += B[i]-(A[i]%B[i])
print(ans)
# Original solution
import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    AB = [0] * N
    for i in range(N):
        AB[i] = list(map(int, input().split()))

    n_press = 0
    for A, B in reversed(AB):
        _, r, = divmod(A + n_press, B)
        n_press += B - r if r > 0 else 0

    ans = n_press
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    AB = [0] * N
    for i in range(N):
        AB[i] = list(map(int, input().split()))

    n_press = 0
    for A, B in reversed(AB):
        r = (A + n_press) % B
        n_press += B - r if r > 0 else 0

    ans = n_press
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
n=int(eval(input()))
a=[0 for _ in range(n)]
b=[0 for _ in range(n)]
for i in range(n):
    a[i],b[i] = list(map(int,input().split()))	
cnt=0
for i in range(n):
    i=n-1-i
    while (a[i]+cnt)%b[i]!=0:
        cnt+=1
print(cnt)


# Optimized solution
n=int(eval(input()))
a=[0 for _ in range(n)]
b=[0 for _ in range(n)]
for i in range(n):
    a[i],b[i] = list(map(int,input().split()))	
cnt=0
for i in range(n):
    i=n-1-i
    if (a[i]+cnt)%b[i]!=0:
        cnt+=b[i]-(a[i]+cnt)%b[i]
print(cnt)
# Original solution
import sys
input = sys.stdin.readline

N = int(eval(input()))
A = [0] * N
B = [0] * N

for i in range(N):
    A[i], B[i] = list(map(int, input().split()))

ans = 0

for i in reversed(list(range(N))):
    if (ans + A[i]) % B[i] != 0:
        ans += ((ans + A[i]) // B[i] + 1) * B[i] - (ans + A[i])

print(ans)

# Optimized solution
import sys
input = sys.stdin.readline

N = int(eval(input()))
A = [0] * N
B = [0] * N

for i in range(N):
    A[i], B[i] = list(map(int, input().split()))

ans = 0

for i in reversed(list(range(N))):
    t = (ans + A[i]) % B[i]
    if t != 0:
        ans += B[i] - t

print(ans)
# Original solution
n = int(eval(input()))
aa = [0]*n
bb = [0]*n
for i in range(n):
    aa[i],bb[i] = list(map(int, input().split()))
ans = 0
for i in range(1,n+1):
    cnt = 0
    if aa[-i] % bb[-i] > 0:
        cnt = ((aa[-i] - 1) // bb[-i] +1) * bb[-i] - aa[-i]
        # print(aa[-i], bb[-i], cnt, aa)
        ans += cnt
        aa = list([x + cnt for x in aa])
    
print(ans)

# Optimized solution
n = int(eval(input()))
aa = [0]*n
bb = [0]*n
for i in range(n):
    aa[i],bb[i] = list(map(int, input().split()))
ans = 0
for i in range(1,n+1):
    cnt = 0
    cur = aa[-i] + ans
    if cur % bb[-i] > 0:
        cnt = ((cur - 1) // bb[-i] +1) * bb[-i] - cur
        # print(cur, bb[-i], cnt, aa)
        ans += cnt
    
print(ans)
# Original solution
N=int(eval(input()))
x=[]
for i in range(N):
    a,b=list(map(int,input().split()))
    x.append([a,b])

x=x[::-1]

ans=0
for i in range(N):
    if x[i][0]%x[i][1]!=0:
        cnt=x[i][1]-x[i][0]%x[i][1]
        ans+=cnt
        for j in range(i+1,N):
            x[j][0]+=cnt
    cnt=0
print(ans)

# Optimized solution
N=int(eval(input()))
r=[]
bi=[]
for i in range(N):
    a,b=list(map(int,input().split()))
    bi.append(b)
    if a%b==0:
        r.append(0)
    else:
        r.append(b-a%b)
push=0
bi=bi[::-1]
r=r[::-1]
for i in range(N):
    if i==0:
        push=r[i]
    else:
        push+=(r[i]-push)%bi[i]
print(push)
# Original solution
n = int(eval(input()))
AB = [[int(i) for i in input().split()] for i in range(n)]

ans = 0
for i in range(n - 1, -1, -1):
    tmp = AB[i][0] % AB[i][1]
    if tmp == 0:
        x = tmp
    else:
        x = AB[i][1] - tmp
    ans += x
    if i > 0:
        for j in range(i - 1, -1, -1):
            AB[j][0] += x
    #print(AB, x, ans)
        
print(ans)

# Optimized solution
n = int(eval(input()))
AB = [[int(i) for i in input().split()] for i in range(n)]

acc = 0
for i in range(n - 1, -1, -1):
    tmp = AB[i][0] % AB[i][1]
    if tmp == 0:
        x = tmp
    else:
        x = AB[i][1] - tmp
    acc += x
    if i > 0:
        AB[i - 1][0] += acc
    #print(AB, x, acc)
        
print(acc)
# Original solution
N = int(eval(input()))
a = list(map(int, input().split()))

aa = [ai - i-1 for i,ai in enumerate(a)]

def tri_search(f:"f(x:float)->float", left, right, iter=100)->float:
    for _ in range(iter):
        ml = (left*2 + right) / 3
        mr = (left + right*2) / 3
        if f(ml) < f(mr):
            right = mr
        else:
            left = ml
    # print(left, right)
    return (right + left) / 2

def f(x):
    ret = 0
    x = round(x)
    for aai in aa:
        ret += abs(aai - x)
    return ret

b = tri_search(f, -10**9, 10**9)
print((f(b)))


# Optimized solution
N = int(eval(input()))
a = list(map(int, input().split()))

aa = [ai - i-1 for i,ai in enumerate(a)]

def tri_search(f:"f(x:float)->float", left, right, iter=100)->float:
    for _ in range(iter):
        ml = (left*2 + right) / 3
        mr = (left + right*2) / 3
        if f(ml) < f(mr):
            right = mr
        else:
            left = ml
    # print(left, right)
    return (right + left) / 2

def f(x):
    ret = 0
    x = int(x+0.5)
    for aai in aa:
        ret += abs(aai - x)
    return ret

b = tri_search(f, -10**9, 10**9)
print((min(f(b), f(b+1), f(b-1))))
# Original solution
n = int(eval(input()))
a= [int(i) for i in input().split()]

for i in range(len(a)):
    a[i]-=i+1

import math
#import statistics
#b=statistics.median(a)
b=math.floor(sum(a)/n)


def p(b,k):
  s=0
  t=0
  for i in a:
      s+=abs(i-b)
      t+=abs(i-b-1) 
  if s>t:
    if k==-1:
      print(t)
    else:
      return p(b+1,1)
  elif s==t:
    print(s)
  else:
    if k==1:
      print(s)
    else:
      return p(b-1,-1)
  
p(b,0)



# Optimized solution
n = int(eval(input()))
a= [int(i) for i in input().split()]

for i in range(len(a)):
    a[i]-=i+1

aa=sorted(a)
if n%2==0:
  b=(aa[int(n/2-1)]+aa[int(n/2)])/2
else:
  b=aa[int((n-1)/2)]

s=0
for i in aa:
  s+=abs(i-b)
print((int(s)))
# Original solution
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]


N = int(eval(input()))
A = list(map(int, input().split()))



def check(b):
    res = 0
    for i in range(N):
        res += abs(A[i] - (b+(i+1)))
    #print(b, res)
    return res

def ternarySearchInt(small, big):
    # 初期条件
    if small == big:
        return small
    elif big - small == 1:
        if check(small) <= check(big):
            return small
        else:
            return big

    def func(small, big):
        midl = (big + small) // 2
        midr = midl + 1
        if big - small == 2:  # 終了条件
            mid = midl
            if check(small) <= check(mid):
                if check(small) <= check(big):
                    return small
                else:
                    return big
            else:
                if check(mid) <= check(big):
                    return mid
                else:
                    return big
        else:
            if check(midl) >= check(midr):
                return func(midl, big)
            else:
                return func(small, midr)
    return func(small, big)

ans = ternarySearchInt(-int(1e9), int(1e9))
#print(check(0))
print((check(ans)))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
for i in range(N):
    A[i] -= i+1
A.sort()
mid = A[N//2]
ans = 0
for i in range(N):
    ans += abs(A[i] - mid)
print(ans)
# Original solution
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり


N = I()
A = LI()
for i in range(N):
    A[i] -= i+1
A.sort()


def f(x):
    return sum(abs(A[i]-x) for i in range(N))


print((f((A[(N-1)//2]+A[N//2])//2)))
# 中間値


# Optimized solution
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()
A = LI()
for i in range(N):
    A[i] -= i+1
A.sort()

x = A[N//2]
y = A[(N-1)//2]
z = (x+y)//2

print((sum(abs(A[i]-z) for i in range(N))))
# Original solution
n=int(eval(input()))
arr=list(map(int,input().split()))
pc=0
mc=0
zc=0
p=[]
m=[]
for i in range(n):
  arr[i]-=(i+1)
  if arr[i]>0:
    pc+=1
    p.append(arr[i])
  elif arr[i]<0:
    mc+=1
    m.append(arr[i])
  else:
    zc+=1
p=sorted(p)
m=sorted(m,reverse=True)
diff=0
while 1:
  if pc+zc>=mc and mc+zc>=pc:
    print((sum(p)+abs(sum(m))+diff))
    break
  else:
    if pc+zc<mc:
      diff+=abs(m[0])*(pc+zc)
      tm=[]
      tmp=abs(m[0])
      pc+=zc
      zc=0
      for i in range(mc):
        m[i]=m[i]+tmp
        if m[i]!=0:
          tm.append(m[i])
        else:
          mc-=1
          zc+=1
      m=tm
    else:
      diff+=p[0]*(mc+zc)
      tp=[]
      tmp=p[0]
      mc+=zc
      zc=0
      for i in range(pc):
        p[i]=p[i]-tmp
        if p[i]!=0:
          tp.append(p[i])
        else:
          pc-=1
          zc+=1

# Optimized solution
n=int(eval(input()))
arr=list(map(int,input().split()))
for i in range(n):
  arr[i]=arr[i]-(i+1)
arr=sorted(arr)
if n%2==1:
  m=arr[n//2]
  ans=0
  for i in range(n):
    ans=ans+abs(arr[i]-m)
  print(ans)
else:
  m1=arr[n//2]
  m2=arr[n//2-1]
  ans1=0
  ans2=0
  for i in range(n):
    ans1=ans1+abs(arr[i]-m1)
    ans2=ans2+abs(arr[i]-m2)
  print((min(ans1,ans2)))
# Original solution
import math
N=int(input().strip())
a=list(map(int, input().strip().split(' ')))
for i in range(N):
    a[i]=a[i]-(i+1)
b=sum(a)/N
ans1=0
ans2=0
for i in range(N):
    ans1+=abs(a[i]-math.floor(sum(a)/N))
for i in range(N):
    ans2+=abs(a[i]-math.ceil(sum(a)/N))


print((min(ans1,ans2)))

# Optimized solution
N=int(input().strip())
a=list(map(int, input().strip().split(' ')))
for i in range(N):
    a[i]=a[i]-i
a.sort()
m=a[(N-1)//2]
ans=0
for i in range(N):
    ans+=abs(a[i]-m)
print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

A = [A[i]-(i+1) for i in range(N)]

ans = 10 ** 9
#print (min(A))
for i in range(min(A), max(A)+1):
    b = [abs(A[j]-i) for j in range(N)]
#    print (sum(b))
#    print (b)
    ans = min(ans, sum(b))

print (ans)

# Optimized solution
#平均値に近づけるという考え方が間違い-->中央値に近づける

N = int(eval(input()))
A = list(map(int, input().split()))

A = [A[i]-(i+1) for i in range(N)]

A.sort()

center1 = A[N//2]
if N == 1:
    center2 = 10 ** 9
else:
    center2 = A[(N+2)//2]

ans1 = 0
for i in range(N):
    ans1 +=abs(A[i]-center1)
    
ans2 = 0
for j in range(N):
    ans2 += abs(A[j]-center2)
    
print((min(ans1, ans2)))
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
from bisect import bisect_left

Ans = 0
pans = 0
low,high = -(10**5),10**9
for i in range(120):
    anslow,anshigh = 0,0
    for i in range(N):
        anslow += abs(A[i]-(i+low))
        anshigh += abs(A[i]-(i+high))
    if anshigh > anslow:
        high=(high+low)//2
    else:
        low=(high+low)//2
    Ans = min(anshigh,anslow)

print(Ans)

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))

for i in range(N):
    A[i] -= i

A.sort()

Ans = 0
mi = len(A)//2 # 中央値のindex
for i in range(N):
    Ans += abs(A[i]-A[mi])

print(Ans)
# Original solution
def bisect(l, r):
    while l < r:
        m = (l + r) // 2
        s1 = sum(abs(am - m) for am in aam)
        s2 = sum(abs(am - m - 1) for am in aam)
        if s1 < s2:
            r = m
        else:
            l = m + 1
    return l


n = int(eval(input()))
aaa = list(map(int, input().split()))
aam = [a - i - 1 for i, a in enumerate(aaa)]
aas = sum(aam)
b = bisect(int(-1e9), int(1e9))
print((sum(abs(am - b) for am in aam)))


# Optimized solution
n = int(eval(input()))
aaa = list(map(int, input().split()))
aam = [a - i - 1 for i, a in enumerate(aaa)]
aam.sort()
b = aam[n // 2]
print((sum(abs(am - b) for am in aam)))
# Original solution
n = int(eval(input()))
a = [int(x) for x in input().split()]
for i in range(n):
    a[i] -= i+1

def hoge(b):
    v = 0
    for x in a:
        v += abs(x-b)
    return v

tmp = float('inf')
for x in set(a):
    if hoge(x) < tmp:
        tmp = hoge(x)

print(tmp)

# Optimized solution
n = int(eval(input()))
a = [int(x) for x in input().split()]
for i in range(n):
    a[i] -= i+1

#中央値を求める
a.sort()
if len(a) % 2 == 0:
    c = (a[len(a)//2] + a[len(a)//2-1]) // 2
else:
    c = a[len(a)//2]

def hoge(b):
    v = 0
    for x in a:
        v += abs(x-b)
    return v

print((hoge(c)))
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
B = sorted(A[i] - i - 1 for i in range(N))
def sad(b):
    return sum(abs(A[i] - i - 1 - b)for i in range(N))
print((min(sad(B[N//2]),sad(B[min(N//2+1,N-1)]))))


# Optimized solution
N=int(eval(input()))
B=sorted(int(x)-i-1 for i,x in enumerate(input().split()))
def sad(x):
    return sum(abs(b-x)for b in B)
print((sad(B[N//2])))
# Original solution
eval(input());S=sorted(int(x)-i-1 for i,x in enumerate(input().split()))
print((sum(abs(S[len(S)//2]-s)for s in S)))


# Optimized solution
_,a=open(0)
S=sorted(int(x)-i for i,x in enumerate(a.split()))
print((sum(abs(S[len(S)//2]-s)for s in S)))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

A_i = [A[i] - i - 1 for i in range(N)]
A_i.sort()
if N % 2 == 1:
    b = A_i[N // 2]
    ans = 0
    for a in A_i:
        ans += abs(a - b)

    print(ans)
else:
    b1 = A_i[N // 2]
    b2 = A_i[N // 2 - 1]
    ans1 = 0
    ans2 = 0
    for a in A_i:
        ans1 += abs(a - b1)
        ans2 += abs(a - b2)
    print((min(ans1, ans2)))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

A_i = [A[i] - i - 1 for i in range(N)]
A_i.sort()
b = A_i[N // 2]
ans = 0
for a in A_i:
    ans += abs(a - b)

print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

A_i = [A[i] - i - 1 for i in range(N)]
A_i.sort()
b = A_i[N // 2]
ans = 0
for a in A_i:
    ans += abs(a - b)

print(ans)


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    A = [a - i - 1 for i, a in enumerate(A)]
    A.sort()
    i = N // 2
    b = A[i]
    ans = 0
    for a in A:
        ans += abs(a - b)
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
import statistics
n = int(eval(input()))
x = list(map(int, input().split()))

for i in range(n):
    x[i] -= i+1

b = statistics.median(x)

ans = 0
for i in range(n):
    ans += abs(x[i] - b)

print((int(ans)))


# Optimized solution
from statistics import median
n = int(eval(input()))
a = list(map(int, input().split()))

for i in range(n):
    a[i] -= i+1

med = median(a)
ans = 0

for i in range(n):
    ans += abs(a[i] - med)

print((int(ans)))