t = int(input())
for i in range(t):
    nd = [int(c) for c in input().split()]
    a  = [int(c) for c in input().split()]
    kq = a[0]
    j = 1
    while nd[1] > 0 and j < nd[0]:
        if nd[1] >= (a[j])*j:
            nd[1] = nd[1] - a[i]*j
            kq += a[j]
            j += 1
        else:
            kq = kq + nd[1]//j
            nd[1] = 0
    print(kq)


t = int(input())
for i in range(t):
    nd = [int(c) for c in input().split()]
    a  = [int(c) for c in input().split()]
    kq = a[0]
    j = 1
    while nd[1] > 0 and j < nd[0]:
        if nd[1] >= (a[j])*j:
            nd[1] = nd[1] - (a[j])*j
            kq += a[j]
            j += 1
        else:
            kq = kq + nd[1]//j
            nd[1] = 0
    print(kq)


# https://codeforces.com/problemset/problem/1307/A
# 1307 A. Cow and Haybales (*800 ELO)

t = int(input())

for _ in range(t):

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

    res = a[0]
    for i in range(1, n):
        max_moves = d // i
        res += min(max_moves, a[i])
        if max_moves <= a[i]:
            break
    print(res)


# https://codeforces.com/problemset/problem/1307/A
# 1307 A. Cow and Haybales (*800 ELO)

t = int(input())

for _ in range(t):

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

    res = a[0]
    for i in range(1, n):
        max_moves = d // i
        moves = min(max_moves, a[i])
        res += moves
        d -= moves * i
        if max_moves <= a[i]:
            break
    print(res)


for _ in range(int(input())):
    n, d = map(int, input().split())
    a = list(map(int, input().split()))
    ans = a[0]
    for i in range(1, len(a)):
        k = a[i] * i
        if k <= d:
            ans += k
            d -= k
        else:
            ans += d // i
            d = 0
    print(ans)


for _ in range(int(input())):
    n, d = map(int, input().split())
    a = list(map(int, input().split()))
    ans = a[0]
    for i in range(1, len(a)):
        while d > 1 and a[i] > 0:
            ans += 1
            a[i] = a[i] - 1
            d -= i

    print(ans)


for _ in range(int(input())):
    n, d = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(len(a)):
        while a[i] > 0:
            ans += (d - 1) > 0
            a[i] -= 1
            d -= i
    print(ans)


for _ in range(int(input())):
    n, d = map(int, input().split())
    a = list(map(int, input().split()))
    ans = a[0]
    for i in range(1, len(a)):
        while a[i] > 0 and d > 0:
            if d - i > 0:
                ans += 1
                d -= i
                a[i] = a[i] - 1
            else:
                break

    print(ans)


for _ in range(int(input())):
    n, d = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(len(a)):
        while a[i] > 0 and d > 0:
            if d - i >= 0:
                ans += 1
                a[i] = a[i] - 1
                d -= i
            else:
                d = 0
    print(ans)


for _ in range(int(input())):
    n,d=map(int,input().split())
    l=list(map(int,input().split()))
    if len(l)==1:
        print(l[0])
    else:
        for i in range(1,len(l)):
            while(i*l[i]>=d):
                l[i]=l[i]-1
        m=max(l[1:])
        print(l[0]+m)


for _ in range(int(input())):
    n,d=map(int,input().split())
    l=list(map(int,input().split()))
    if len(l)==1:
        print(l[0])
    else:
        for i in range(1,len(l)):
            mi=min(i*l[i],d)
            d=d-mi
            l[0]=l[0]+mi//i
        print(l[0])


for _ in range(int(input())):
    n,d=map(int,input().split())
    l=list(map(int,input().split()))
    if len(l)==1:
        print(l[0])
    else:
        for i in range(1,len(l)):
            while(i*l[i]>d):
                l[i]=l[i]-1
        m=max(l[1:])
        print(l[0]+m)


testCases = int(input())
ret = []
for cases in range(testCases):
piles, days = map(int, input().split(" "))
hay = list(map(int, input().split(" ")))
if piles == 1:
    ret.append(hay[0])
else:
    for i in range(days):
        for j in range(1, len(hay)):
            if abs((hay[j - 1] - hay[j])) > 0 and hay[j] != 0:
                hay[j - 1] = hay[j - 1] + 1
                hay[j] = hay[j] - 1
                break
    ret.append(hay[0])
for i in ret:
print(i)


testCases = int(input())
ret = []
for cases in range(testCases):
    piles, days = map(int, input().split(" "))
    hay = list(map(int, input().split(" ")))
    if piles == 1:
        ret.append(hay[0])
    else:
        for i in range(days):
            for j in range(1, len(hay)):
                if abs((hay[j - 1] - hay[j])) > 0 and hay[j] != 0:
                    hay[j - 1] = hay[j - 1] + 1
                    hay[j] = hay[j] - 1
                    break
        ret.append(hay[0])
print(ret)


testCases = int(input())
ret = []
for cases in range(testCases):
    piles, days = map(int, input().split(" "))
    hay = list(map(int, input().split(" ")))
    if piles == 1:
        ret.append(hay[0])
    else:
        for i in range(days):
            for j in range(1, len(hay)):
                if hay[j] != 0:
                    hay[j - 1] = hay[j - 1] + 1
                    hay[j] = hay[j] - 1
                    break
        ret.append(hay[0])
for i in ret:
    print(i)


testCases = int(input())
ret = []
for cases in range(testCases):
    piles, days = map(int, input().split(" "))
    hay = list(map(int, input().split(" ")))
    if piles == 1:
        ret.append(hay[0])
    else:
        for i in range(days):
            for j in range(1, len(hay)):
                if abs((hay[j - 1] - hay[j])) > 0 and hay[j] != 0:
                    hay[j - 1] = hay[j - 1] + 1
                    hay[j] = hay[j] - 1
                    break
        ret.append(hay[0])
for i in ret:
    print(i)


for _ in range(int(input())):
    n,d = map(int,input().split())
    ai = list(map(int,input().split()))
    for j in range(d):
        for i in range(1,n):
            if ai[i] > 0:
                ai[i] -= 1
                ai[i-1] += 1
                break
    print(max(ai))


for _ in range(int(input())):
    n,d = map(int,input().split())
    ai = list(map(int,input().split()))
    for j in range(d):
        for i in range(1,n):
            if ai[i] > 0:
                ai[i] -= 1
                ai[i-1] += 1
                break
    print(ai[0])


for _ in range(int(input())):
    n,d = map(int,input().split())
    ai = list(map(int,input().split()))
    print(max(ai)+min(ai))


cases = int(input().strip())

for case in range(cases):
	n, d = list(map(int, input().strip().split(" ")))
	a = list(map(int, input().strip().split(" ")))
	done = False
	for i in range(1, n):
		while a[i] > 0:
			if d > i:
				d -= i
				a[0] += 1
				a[i] -= 1
			else:
				done = True
				break
		if done:
			break
	print(a[0])


cases = int(input().strip())

for case in range(cases):
	n, d = list(map(int, input().strip().split(" ")))
	a = list(map(int, input().strip().split(" ")))
	done = False
	for i in range(1, n):
		while a[i] > 0:
			if d >= i:
				d -= i
				a[0] += 1
				a[i] -= 1
			else:
				done = True
				break
		if done:
			break
	print(a[0])


t = int(input())
for _ in range(t):
    n, d = [int(i) for i in input().strip().split()]
    piles = [int(i) for i in input().strip().split()]
    sm = piles[0]
    for i in range(1, n):
        if int(d / i) == 0:
            break
        tmp =min(int(d / i), piles[i])
        d -= tmp
        sm += tmp
    print(sm)


t = int(input())
for _ in range(t):
    n, d = [int(i) for i in input().strip().split()]
    piles = [int(i) for i in input().strip().split()]
    sm = piles[0]
    for i in range(1, n):
        if int(d / i) == 0:
            break
        tmp = min(int(d / i), piles[i])
        d -= (i * tmp)
        sm += tmp
    print(sm)


t = int(input())
while t--:
    n,d = map(int,input().split())
    a = list(map(int,input().split()))
    count = a[0]
    for i in range(1,len(a)):
        k = a[i]
        while k!=0 :
            if d==0:
                break
            count++
            d-=i
            k--
        if d==0:
            break
    return count


t = int(input())
while t:
    n,d = map(int,input().split())
    a = list(map(int,input().split()))
    count = a[0]
    for i in range(1,len(a)):
        k = a[i]
        #print("k->",k)
        while k!=0 :
            #print("d->",d)
            if d<i:
                break
            count+=1
            d-=i
            k-=1
        #print("c->",count)
        if d==0:
            break
    print(count)
    t-=1


t=int(input())
while(t!=0):
    n,d=map(int,input().split())
    a=[int(x) for x in input().split()]
    for i in range(1,len(a)):
        tmp=min(d,a[i])
        d=d-(tmp*i)
        a[0]+=tmp
        if (d==0):
            break;
    print(a[0])


t=int(input())
while(t!=0):
    n,d=map(int,input().split())
    a=[int(x) for x in input().split()]
    for i in range(1,len(a)):
        tmp=min(d//i,a[i])
        d-=(tmp*i)
        a[0]+=tmp
    print(a[0])
    t-=1


t=int(input())
while(t!=0):
    n,d=map(int,input().split())
    a=[int(x) for x in input().split()]
    for i in range(1,len(a)):
        tmp=min(d//i,a[i])
        d-=(tmp*i)
        a[0]+=tmp
    print(a[0])


t=int(input())
while(t!=0):
    n,d=map(int,input().split())
    a=[int(x) for x in input().split()]
    for i in range(1,len(a)):
        tmp=min(d,a[i])
        d=d-(tmp*i)
        a[0]+=tmp
        if (d==0):
            break;
    print(a[0])
    t-=1


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


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


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


t = int(input())
numh=[]
numd=[]

for i in range(t):
    numh.append(list(map(int,input().split())))
    numd.append(list(map(int,input().split())))
for i in range(t):
    print(numh[i])
    print(numd[i])
    piles = numh[i][0]
    day = numh[i][1]
    print(piles,day)
    print(len(numd[i]))

for i in range(t):
    day=numh[i][1]
    val=numd[i][0]
    for j in range(1,len(numd[i])):
        if(day < (j*numd[i][j])):
            val+= (day//j)
        else:
            val+=(numd[i][j]//j)
            day -= (j * numd[i][j])
    print(val)


t = int(input())
numh=[]
numd=[]

for i in range(t):
    numh.append(list(map(int,input().split())))
    numd.append(list(map(int,input().split())))

for i in range(t):
    piles=numh[i][0]
    day=numh[i][1]
    val=numd[i][0]
    for j in range(1,len(numd[i])):
        if(day < (j*numd[i][j])):
            val+= (day//j)
            day -= (j * numd[i][j])
            break
        else:
            val+=(numd[i][j]//j)
            day -= (j * numd[i][j])
    print(val)


t = int(input())
numh=[]
numd=[]

for i in range(t):
    numh.append(list(map(int,input().split())))
    numd.append(list(map(int,input().split())))

for i in range(t):
    day=numh[i][1]
    val=numd[i][0]
    for j in range(1,len(numd[i])):
        if(day < (j*numd[i][j])):
            val+= (day//j)
            break
        else:
            val+=(numd[i][j]//j)
            day -= (j * numd[i][j])
    print(val)


t = int(input())
numh=[]
numd=[]

for i in range(t):
    numh.append(list(map(int,input().split())))
    numd.append(list(map(int,input().split())))

for i in range(t):
    day=numh[i][1]
    val=numd[i][0]
    for j in range(1,len(numd[i])):
        if(day < (j*numd[i][j])):
            #print(j, day, val)
            val+= (day//j)
            #print(j, day, val)
            break
        else:
            #print(j, day, val)
            val+=(numd[i][j])
            day -= (j * numd[i][j])
            #print(j, day, val)

    print(val)


for _ in range(int(input())):
    n, d = map(int,input().split())
    a = list(map(int,input().split()))
    if n == 1:
        print(a[0])
    else:
        max_value = max(a[1:])
        max_index = a.index(max_value)
        for i in range(d):
            if max_value == 0:
                break
            else:
                if d <= max_index:
                    break
                d = d - max_index
                a[0] += 1
                #print(a[0],d)
        print(a[0])


for _ in range(int(input())):
    n, d = map(int,input().split())
    a = list(map(int,input().split()))
    if n == 1:
        print(a[0])
    else:
        i = 0
        for i in range(1,d):
            if i == n:
                break
            else:
                if a[i] != 0:
                    d -= i
                    if d < 0:
                        break
                    a[0] += 1
                    a[i] -= 1
                #print(a[0],d)
        print(a[0])


for _ in range(int(input())):
    n, d = map(int,input().split())
    a = list(map(int,input().split()))
    if n == 1:
        print(a[0])
    else:
        for i in range(n):
            max_value = max(a[1:])
            if max_value == 0:
                break
            max_index = a.index(max_value)
            a[max_index-1] += 1
            a[max_index] -= 1
        print(max(a))


for _ in range(int(input())):
    n, d = map(int,input().split())
    a = list(map(int,input().split()))
    if n == 1:
        print(a[0])
    else:
        i = 1
        while (d > 0):
            if a[i] != 0:
                d -= i
                if d < 0:
                    break
                a[0] += 1
                a[i] -= 1
            else:
                i += 1
            if i == n:
                break
        print(a[0])


import sys
input = sys.stdin.readline

for _ in range(int(input())):
    n, d = map(int, input().split())
    lst = list(map(int, input().split()))
    cnt = 0
    for i in lst[1:]:
        if lst[i] * i > d:
            cnt += d // i
            break
        cnt += lst[i]
    print(lst[0] + cnt)


import sys
input = sys.stdin.readline

for _ in range(int(input())):
    n, d = map(int, input().split())
    lst = list(map(int, input().split()))
    cnt = 0
    for i in range(len(lst)):
        if lst[i] * i > d:
            cnt += d // i
            break
        cnt += lst[i]
        d -= lst[i] * i
    print(cnt)


import sys
input = sys.stdin.readline

for _ in range(int(input())):
    n, d = map(int, input().split())
    lst = list(map(int, input().split()))
    cnt = 0
    for i in range(len(lst)):
        if lst[i] * i > d:
            cnt += d // i
            break
        cnt += lst[i]
    print(cnt)


def answer():
    n = int(input())
    while n:
        a = [int(x) for x in input().split()]
        d = a[1]
        b = [int(x) for x in input().split()]
        i=1
        time=0
        ans=b[0]
        while i<len(b):
            targ=b[i]
            while b[i]>0 and time<d:
                b[i]-=1
                ans+=1
                time+=i
            i+=1

        print(ans)

        n-=1
answer()


def answer():
    n = int(input())
    while n:
        a = [int(x) for x in input().split()]
        d = a[1]
        b = [int(x) for x in input().split()]
        x=b[0]
        b = b[1:]
        i=0
        while i<len(b):
            b[i]=max(0,b[i]-max(0,1+i-d))
            i+=1
        print(b)
        c = sum(b)
        print(min(c+x,d+x))
        n-=1
answer()


def answer():
    n = int(input())
    while n:
        a = [int(x) for x in input().split()]
        d = a[1]
        b = [int(x) for x in input().split()]
        x=b[0]
        b = b[1:]
        i=0
        while i<len(b):
            b[i]=max(0,b[i]-i)
            i+=1
        print(b)
        c = sum(b)
        print(min(c+x,d+x))
        n-=1
answer()


def answer():
    n = int(input())
    while n:
        a = [int(x) for x in input().split()]
        d = a[1]
        b = [int(x) for x in input().split()]
        i=1
        time=0
        ans=b[0]
        while i<len(b) and time<=d:
            while b[i]>0 and time+i<=d:
                b[i]-=1
                time+=i
                ans+=1
               # print(b,time,ans)

            i+=1

        print(ans)

        n-=1
answer()


def answer():
    n = int(input())
    while n:
        a = [int(x) for x in input().split()]
        d = a[1]
        b = [int(x) for x in input().split()]
        x=b[0]
        b = b[1:]
        i=0
        while i<len(b):
            b[i]=max(0,b[i]-i)
            i+=1
        #print(b)
        c = sum(b)
        print(min(c+x,d+x))
        n-=1
answer()


from sys  import stdin,stdout

import bisect

import math

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

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

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

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

def pr(n):
    stdout.write(str(n)+"\n")

def soe(limit):
    l=[1]*(limit+1)
    prime=[]
    for i in range(2,limit+1):
        if l[i]:
            for j in range(i*i,limit+1,i):
                l[j]=0

    for i in range(2,limit+1):
        if l[i]:
            prime.append(i)
    return prime

def segsoe(low,high):
    limit=int(high**0.5)+1
    prime=soe(limit)
    n=high-low+1
    l=[0]*(n+1)
    for i in range(len(prime)):
        lowlimit=(low//prime[i])*prime[i]
        if lowlimit<low:
            lowlimit+=prime[i]
        if lowlimit==prime[i]:
            lowlimit+=prime[i]
        for j in range(lowlimit,high+1,prime[i]):
            l[j-low]=1
    for i in range(low,high+1):
        if not l[i-low]:
            if i!=1:
                print(i)

def power(a,n):
    r=1
    while n:
        if n&1:
            r=(r*a)
            n=n-1
        else:
            a=(a*a)
            n=n>>1
    return r

def solve():
    n,k=mp()
    l=li()
    d=0
    c=n-1
    for i in range(1,n):
        if l[i]>0:
            c=i
            break
    while l[c]>0 and k>=c:
        l[c]-=1
        k-=c
        d+=1
    print(l[0]+d)

for _ in range(inp()):
    solve()


from sys  import stdin,stdout

import bisect

import math

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

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

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

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

def pr(n):
    stdout.write(str(n)+"\n")

def soe(limit):
    l=[1]*(limit+1)
    prime=[]
    for i in range(2,limit+1):
        if l[i]:
            for j in range(i*i,limit+1,i):
                l[j]=0

    for i in range(2,limit+1):
        if l[i]:
            prime.append(i)
    return prime

def segsoe(low,high):
    limit=int(high**0.5)+1
    prime=soe(limit)
    n=high-low+1
    l=[0]*(n+1)
    for i in range(len(prime)):
        lowlimit=(low//prime[i])*prime[i]
        if lowlimit<low:
            lowlimit+=prime[i]
        if lowlimit==prime[i]:
            lowlimit+=prime[i]
        for j in range(lowlimit,high+1,prime[i]):
            l[j-low]=1
    for i in range(low,high+1):
        if not l[i-low]:
            if i!=1:
                print(i)

def power(a,n):
    r=1
    while n:
        if n&1:
            r=(r*a)
            n=n-1
        else:
            a=(a*a)
            n=n>>1
    return r

def solve():
    n,k=mp()
    l=li()
    for i in range(k):
        for j in range(1,n):
            if l[j]>0:
                l[j]-=1
                l[j-1]+=1
                break
    print(l[0])

for _ in range(inp()):
    solve()


for _ in range(int(input())):
    n,d=list(map(int,input().split()))
    a=list(map(int,input().split()))
    if n>1:
        i=1
        while d>i:
            if a[i]!=0:
                if d-i>=0:
                    a[0]+=1
                    d-=i
                else:
                    break
            else:
                if i+1<n:
                    i+=1
                else:
                    break
        print(a[0])
    else:
        print(a[0])


for _ in range(int(input())):
    n,d=list(map(int,input().split()))
    a=list(map(int,input().split()))
    i=1
    if n>1:
        while d>=i and i<n:
            if a[i]!=0:
                a[0]+=1
                a[i]-=1
                d-=i
            else:
                i+=1
        print(a[0])
        #print(d,i)
    else:
        print(a[0])


for _ in range(int(input())):
    n,d=list(map(int,input().split()))
    a=list(map(int,input().split()))
    if n>1:
        i=1
        while d>i:
            if a[i]!=0:
                a[0]+=1
                d=d-(i)
            else:
                if i+1<n:
                    i+=1
                else:
                    break
        print(a[0])
    else:
        print(a[0])


for _ in range(int(input())):
    n,d=list(map(int,input().split()))
    a=list(map(int,input().split()))
    if n>1:
        i=1
        while d>i:
            if a[i]!=0:
                a[0]+=1
                d=d-(i)
            else:
                i+=1
        print(a[0])
    else:
        print(a[0])


#include <iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<set>
#include<list>
#include<cstdlib>
#include<cstdio>
#include<iomanip>
#include<stack>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<map>
#include<sstream>
#include<assert.h>

using namespace std;

const int maxN = 100010;
bool isP[maxN];
int pre[maxN];

void doit(){
    for(int i = 0; i < maxN; i++)
        isP[i] = true;
    isP[0] = isP[1] = false;
    for(int p = 2; p < maxN; p++){
        if(isP[p])
            for(int d = 2; d * p < maxN; d++)
                isP[d * p] = false;
    }

    pre[0] = pre[1] = -1;
    pre[2] = 2;
    pre[3] = 3;
    for(int k = 4; k < maxN; k++){
        if(k % 2 == 0){
            pre[k] = 2;
        }
        else{
            for(int j = 1; j <= k; j++)
                if(isP[j + 1] && isP[k - j]){
                    pre[k] = j + 1;
                    break;
                }
        }
    }
}

int main(){
//freopen("C.in","r",stdin);
    doit();
    int n;
    cin >> n;
    pair<int,int> a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a,a + n);

    int where[n];
    int who[n];
    for(int i = 0; i < n; i++){
        who[a[i].second] = i;
        where[i] = a[i].second;
    }

    //for(int i = 0; i < n; i++)
        //cerr << who[i] << " " << where[i] << endl;

    vector<pair<int,int> > res;

    for(int k = n - 1; k >= 0; k--){
        ///mando k a la posicion k
        int pos = where[k];
        int delta = k - pos + 1;
assert(delta >= 0);
        int p = pre[delta];
        while(p != -1){
            int other = who[pos + p - 1];
cerr << k << " " << pos << endl;
assert(pos + p - 1 < n);
assert(other < n);
assert(k < n);
            where[other] = pos;
            where[k] = pos + p - 1;
            who[pos] = other;
            who[pos + p - 1] = k;
            res.push_back(make_pair(pos + 1, pos + p));
            pos = pos + p - 1;
            delta = delta - p + 1;
            p = pre[delta];
        }
    }

    cout << res.size() << endl;
    for(int i = 0; i < (int)res.size(); i++)
        cout << res[i].first << " " << res[i].second << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
//#include <cassert>
#include <cstring>
#include <sstream>
#include <ctime>
#include <stdexcept>
#include <limits>
#include <bitset>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

#define assert(x) if (!(x)) cout << "ASSERT FAILED ON {"#x"}! Line: " << __LINE__ << "\n" << flush, exit(1);
#define assertwt(x) if (!(x)) {cout << "ASSERT FAILED ON {"#x"}! Line: " << __LINE__ << "\n" << flush; while(1);}

#define input_file(s) assert(freopen(s, "r", stdin))
#define output_file(s) assert(freopen(s, "w", stdout))
#define stderr_file(s) assert(freopen(s, "w", stderr))

#ifdef MY
#define FLU cout << flush
#else
#define FLU 42
#endif

#define ALL(a) (a).begin(), (a).end()
#define FORAB(i, a, b) for (int i = a; i < b; ++i)
#define DBG(x) z_dbg << #x" = " << x << "\n", FLU
#ifdef MY
#define CHECKPOINT z_dbg << "At line " << __LINE__ << ", clock: " << clock() << "\n", FLU
#else
#define CHECKPOINT 42
#endif

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ipair;

const double PI = 3.14159265358979323846;
const double EPS = 1e-9;

const int INF = 2e9;
const ll LINF = 1e18;

template<class T>
const T sqr(const T &x) { return x * x; }

template<class T>
const T &min(const T &a, const T &b, const T &c) { return min(min(a, b), c); }

template<class T>
const T &max(const T &a, const T &b, const T &c) { return max(max(a, b), c); }

struct DbgStream {
    template <class T>
    DbgStream &operator<<(const T &x) {
        #ifdef MY
        cout << x;
        #endif
        return *this;
    }
} z_dbg;

#define cerrr cerr
#define cerr z_dbg

// !GLOB

bool si[100500];

int n, a[100500], w[100500];

vector<ipair> hist;

void swp(int i, int j) {
    assert(!si[j - i + 1]);
    hist.push_back({i, j});
    w[a[i]] = j;
    w[a[j]] = i;
    swap(a[i], a[j]);
}

void sw(int i, int j) {
    if (!si[j - i + 1]) {
        // prime, just swap
        swp(i, j);
        return;
    }
    if (j - i + 3 <= 10) {
        for (int k = 2; k < j - i + 1; ++k) {
            if (!si[k] && !si[j - i + 2 - k]) {
                int mid = i + k - 1;
                swp(mid, j);
                swp(i, mid);
                swp(mid, j);
                return;
            }
        }
        cerr << "Y U WORK WRONNG?!\n";
        exit(1);
    }
    int a = -1, b;
    int q = j - i + 3;
    for (int c = 2; c < q - 1; ++c) {
        if (si[c]) continue;
        for (int d = 2; d < q - c - 1; ++d) {
            if (si[d] || si[q - c - d]) continue; // YA LALKA
            a = i + c - 1;
            b = a + d - 1;
            break;
        }
        if (a != -1) break;
    }
    swp(i, a);
    swp(a, b);
    swp(b, j);
    swp(a, b);
    swp(i, a);
}

int main() {
    #ifdef MY
    input_file("input.txt");
    //output_file("output.txt");
    //stderr_file("stderr.txt");
    #else
    #define TASK_FILE_NAME "protocol"
    //input_file(TASK_FILE_NAME".in");
    //output_file(TASK_FILE_NAME".out");
    //input_file("input.txt");
    //output_file("output.txt");
    #endif

    ios_base::sync_with_stdio(0);

    // !MAIN

    for (int i = 2; i < 100500; ++i) {
        if (si[i]) continue;
        for (int j = i * 2; j < 100500; j += i) {
            si[j] = 1;
        }
    }

    cin >> n;

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

    for (int i = 0; i < n - 1; ++i) {
        if (a[i] != i + 1) sw(i, w[i + 1]);
    }

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

    cout << hist.size() << "\n";
    for (ipair &e : hist) {
        cout << e.first + 1 << " " << e.second + 1 << "\n";
    }

    return 0;
}

/*

    for (int i = 3; i <= 100000; ++i) {
        if (si[i]) {
            bool has = 0;
            for (int j = 2; j < i - 1; ++j) {
                if (si[j]) continue;
                for (int k = 2; k < j - 1; ++k) {
                    if (i - j - k > 2 && !si[k] && !si[i - j - k]) {
                        cout << i << " = " << j << "+" << k << "+" << i - j - k << "\n";
                        has = 1;
                        break;
                    }
                }
                if (has) break;
            }
            if (!has) cout << i << " !has" << endl;
        }
    }
*/


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
//#include <cassert>
#include <cstring>
#include <sstream>
#include <ctime>
#include <stdexcept>
#include <limits>
#include <bitset>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

#define assert(x) if (!(x)) cout << "ASSERT FAILED ON {"#x"}! Line: " << __LINE__ << "\n" << flush, exit(1);
#define assertwt(x) if (!(x)) {cout << "ASSERT FAILED ON {"#x"}! Line: " << __LINE__ << "\n" << flush; while(1);}

#define input_file(s) assert(freopen(s, "r", stdin))
#define output_file(s) assert(freopen(s, "w", stdout))
#define stderr_file(s) assert(freopen(s, "w", stderr))

#ifdef MY
#define FLU cout << flush
#else
#define FLU 42
#endif

#define ALL(a) (a).begin(), (a).end()
#define FORAB(i, a, b) for (int i = a; i < b; ++i)
#define DBG(x) z_dbg << #x" = " << x << "\n", FLU
#ifdef MY
#define CHECKPOINT z_dbg << "At line " << __LINE__ << ", clock: " << clock() << "\n", FLU
#else
#define CHECKPOINT 42
#endif

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ipair;

const double PI = 3.14159265358979323846;
const double EPS = 1e-9;

const int INF = 2e9;
const ll LINF = 1e18;

template<class T>
const T sqr(const T &x) { return x * x; }

template<class T>
const T &min(const T &a, const T &b, const T &c) { return min(min(a, b), c); }

template<class T>
const T &max(const T &a, const T &b, const T &c) { return max(max(a, b), c); }

struct DbgStream {
    template <class T>
    DbgStream &operator<<(const T &x) {
        #ifdef MY
        cout << x;
        #endif
        return *this;
    }
} z_dbg;

#define cerrr cerr
#define cerr z_dbg

// !GLOB

bool si[100500];

int n, a[100500], w[100500];

vector<ipair> hist;

void swp(int i, int j) {
    hist.push_back({i, j});
    w[a[i]] = j;
    w[a[j]] = i;
    swap(a[i], a[j]);
}

void sw(int i, int j) {
    if (!si[j - i + 1]) {
        // prime, just swap
        swp(i, j);
        return;
    }
    if (j - i + 3 <= 10) {
        for (int k = 2; k < j - i + 1; ++k) {
            if (!si[k] && !si[j - i + 2 - k]) {
                int mid = i + k - 1;
                swp(mid, j);
                swp(i, mid);
                swp(mid, j);
                return;
            }
        }
        cerr << "Y U WORK WRONNG?!\n";
        exit(1);
    }
    int a = -1, b;
    int q = j - i + 3;
    for (int c = 2; c < q - 1; ++c) {
        if (si[c]) continue;
        for (int d = 2; d < q - c - 1; ++d) {
            if (si[d]) continue;
            a = i + c - 1;
            b = a + d - 1;
            break;
        }
        if (a != -1) break;
    }
    swp(i, a);
    swp(a, b);
    swp(b, j);
    swp(a, b);
    swp(i, a);
}

int main() {
    #ifdef MY
    input_file("input.txt");
    //output_file("output.txt");
    //stderr_file("stderr.txt");
    #else
    #define TASK_FILE_NAME "protocol"
    //input_file(TASK_FILE_NAME".in");
    //output_file(TASK_FILE_NAME".out");
    //input_file("input.txt");
    //output_file("output.txt");
    #endif

    ios_base::sync_with_stdio(0);

    // !MAIN

    for (int i = 2; i < 100500; ++i) {
        if (si[i]) continue;
        for (int j = i * 2; j < 100500; j += i) {
            si[j] = 1;
        }
    }

    cin >> n;

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

    for (int i = 0; i < n - 1; ++i) {
        if (a[i] != i + 1) sw(i, w[i + 1]);
    }

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

    cout << hist.size() << "\n";
    for (ipair &e : hist) {
        cout << e.first + 1 << " " << e.second + 1 << "\n";
    }

    return 0;
}

/*

    for (int i = 3; i <= 100000; ++i) {
        if (si[i]) {
            bool has = 0;
            for (int j = 2; j < i - 1; ++j) {
                if (si[j]) continue;
                for (int k = 2; k < j - 1; ++k) {
                    if (i - j - k > 2 && !si[k] && !si[i - j - k]) {
                        cout << i << " = " << j << "+" << k << "+" << i - j - k << "\n";
                        has = 1;
                        break;
                    }
                }
                if (has) break;
            }
            if (!has) cout << i << " !has" << endl;
        }
    }
*/


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
//#include <cassert>
#include <cstring>
#include <sstream>
#include <ctime>
#include <stdexcept>
#include <limits>
#include <bitset>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

#define assert(x) if (!(x)) cout << "ASSERT FAILED ON {"#x"}! Line: " << __LINE__ << "\n" << flush, exit(1);
#define assertwt(x) if (!(x)) {cout << "ASSERT FAILED ON {"#x"}! Line: " << __LINE__ << "\n" << flush; while(1);}

#define input_file(s) assert(freopen(s, "r", stdin))
#define output_file(s) assert(freopen(s, "w", stdout))
#define stderr_file(s) assert(freopen(s, "w", stderr))

#ifdef MY
#define FLU cout << flush
#else
#define FLU 42
#endif

#define ALL(a) (a).begin(), (a).end()
#define FORAB(i, a, b) for (int i = a; i < b; ++i)
#define DBG(x) z_dbg << #x" = " << x << "\n", FLU
#ifdef MY
#define CHECKPOINT z_dbg << "At line " << __LINE__ << ", clock: " << clock() << "\n", FLU
#else
#define CHECKPOINT 42
#endif

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ipair;

const double PI = 3.14159265358979323846;
const double EPS = 1e-9;

const int INF = 2e9;
const ll LINF = 1e18;

template<class T>
const T sqr(const T &x) { return x * x; }

template<class T>
const T &min(const T &a, const T &b, const T &c) { return min(min(a, b), c); }

template<class T>
const T &max(const T &a, const T &b, const T &c) { return max(max(a, b), c); }

struct DbgStream {
    template <class T>
    DbgStream &operator<<(const T &x) {
        #ifdef MY
        cout << x;
        #endif
        return *this;
    }
} z_dbg;

#define cerrr cerr
#define cerr z_dbg

// !GLOB

bool si[100500];

int n, a[100500], w[100500];

vector<ipair> hist;

void swp(int i, int j) {
    hist.push_back({i, j});
    w[a[i]] = j;
    w[a[j]] = i;
    swap(a[i], a[j]);
}

void sw(int i, int j) {
    if (!si[j - i + 1]) {
        // prime, just swap
        swp(i, j);
        return;
    }
    if (j - i + 3 <= 10) {
        for (int k = 2; k < j - i + 1; ++k) {
            if (!si[k] && !si[j - i + 2 - k]) {
                int mid = i + k - 1;
                swp(mid, j);
                swp(i, mid);
                swp(mid, j);
                return;
            }
        }
        cerr << "Y U WORK WRONNG?!\n";
        exit(1);
    }
    int a, b;
    if (!si[j - i + 3 - 5 - 2]) { // j - i + 3 = 5 + 2 + p
        a = i + 1;
        b = i + 5;
    } else { // j - i - 3 = 5 + 3 + p
        a = i + 2;
        b = i + 6;
    }
    swp(i, a);
    swp(a, b);
    swp(b, j);
    swp(a, b);
    swp(i, a);
}

int main() {
    #ifdef MY
    input_file("input.txt");
    //output_file("output.txt");
    //stderr_file("stderr.txt");
    #else
    #define TASK_FILE_NAME "protocol"
    //input_file(TASK_FILE_NAME".in");
    //output_file(TASK_FILE_NAME".out");
    //input_file("input.txt");
    //output_file("output.txt");
    #endif

    ios_base::sync_with_stdio(0);

    // !MAIN

    for (int i = 2; i < 100500; ++i) {
        if (si[i]) continue;
        for (int j = i * 2; j < 100500; j += i) {
            si[j] = 1;
        }
    }

    cin >> n;

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

    for (int i = 0; i < n - 1; ++i) {
        if (a[i] != i + 1) sw(i, w[i + 1]);
    }

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

    cout << hist.size() << "\n";
    for (ipair &e : hist) {
        cout << e.first + 1 << " " << e.second + 1 << "\n";
    }

    return 0;
}


#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAX=(int)1e5+4;
bool ispr[MAX];
int pr[MAX], pn, a[MAX], pos[MAX];
pair <int, int> b[MAX];
void init() {
  fill(ispr, ispr+MAX, true);
  for (int k = 2; k < MAX; k++) {
    if (ispr[k]) {
      pr[pn++] = k;
    }
    for (int i = 0; i < pn && k * pr[i] < MAX; i++) {
      ispr[k*pr[i]] = false;
      if (k%pr[i] == 0) break;
    }
  }
}

int main() {
  init();
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
    b[i].first = a[i];
    b[i].second = i;
  }
  sort(b, b+n);
  for (int i = 0; i < n; i++) pos[b[i].second] = i;
  bool flag = false;
  while (1) {
    if (flag) break;
    int remain = 0;
    for (int i = 0; i < n; i++) {
      //printf("a[%d] = %d\n", i+1, a[i]);
      if (pos[i] == i) { remain++; continue;}
      if (pos[i] > i) {
        int tmp = pos[i] - i + 1;
        //printf("tmp: %d\n", tmp);
        int j = upper_bound(pr, pr+pn, tmp) - pr - 1;
        //printf("pr: %d\n", pr[j]);
        int target = i + pr[j] - 1;
        swap(pos[target], pos[i]);
        swap(a[target], a[i]);
        printf("%d %d\n", i+1, target+1);
      } else {
        int tmp = i - pos[i] + 1;
        //printf("tmp: %d\n", tmp);
        int j = upper_bound(pr, pr+pn, tmp) - pr - 1;
        //printf("pr: %d\n", pr[j]);
        int target = i - pr[j] + 1;
        swap(pos[target], pos[i]);
        swap(a[target], a[i]);
        printf("%d %d\n", target+1, i+1);
      }
    }
    flag = (remain == n);
  }
  return 0;
}


#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAX=(int)1e7+4;
bool ispr[MAX];
int pr[MAX], pn, a[MAX], pos[MAX], n;
vector < pair<int,int> > ans;
pair <int, int> b[MAX];
bool visit[MAX];
void init() {
  fill(ispr, ispr+MAX, true);
  for (int k = 2; k < MAX; k++) {
    if (ispr[k]) {
      pr[pn++] = k;
    }
    for (int i = 0; i < pn && k * pr[i] < MAX; i++) {
      ispr[k*pr[i]] = false;
      if (k%pr[i] == 0) break;
    }
  }
}
int dfs(int i) {
  visit[i] = 1;
  int ret = i;
  if (pos[i] > i) {
    int tmp = pos[i] - i + 1;
    int j = upper_bound(pr, pr+n, tmp) -pr - 1;
    int target = i + pr[j] - 1;
    if (visit[target] == 0) {
      ret = dfs(target);
      swap(a[i], a[target]);
      swap(pos[i], pos[target]);
      ans.push_back(make_pair(i, target));
    }
  } else {
    int tmp = i - pos[i] + 1;
    int j = upper_bound(pr, pr+n, tmp) -pr - 1;
    int target = i - pr[j] + 1;
    if (visit[target] == 0) {
      ret = dfs(target);
      swap(a[i], a[target]);
      swap(pos[i], pos[target]);
      ans.push_back(make_pair(i, target));
    }
  }
  return ret;
}

int main() {
  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
    b[i].first = a[i];
    b[i].second = i;
  }
  sort(b, b+n);
  for (int i = 0; i < n; i++) pos[b[i].second] = i;
  bool flag = false;
  while (1) {
    if (flag) break;
    int remain = 0;
    fill(visit, visit + n, 0);
    for (int i = 0; i < n; i++) {
      //printf("a[%d] = %d\n", i+1, a[i]);
      if (pos[i] == i) { visit[i] = 2; remain++; continue;}
      if (!visit[i]) dfs(i);
    }
    flag = (remain == n);
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first+1, ans[i].second+1);
  return 0;
}


#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAX=(int)1e5+4;
bool ispr[MAX];
int pr[MAX], pn, a[MAX], pos[MAX], n;
vector < pair<int,int> > ans;
pair <int, int> b[MAX];
bool visit[MAX];
void init() {
  fill(ispr, ispr+MAX, true);
  for (int k = 2; k < MAX; k++) {
    if (ispr[k]) {
      pr[pn++] = k;
    }
    for (int i = 0; i < pn && k * pr[i] < MAX; i++) {
      ispr[k*pr[i]] = false;
      if (k%pr[i] == 0) break;
    }
  }
}
void dfs(int i) {
  visit[i] = 1;
  if (pos[i] > i) {
    int tmp = pos[i] - i + 1;
    int j = upper_bound(pr, pr+n, tmp) - pr - 1;
    int target = i + pr[j] - 1;
    if (visit[target] == 0) {
      swap(a[i], a[target]);
      swap(pos[i], pos[target]);
      ans.push_back(make_pair(i, target));
    }
  } else {
    int tmp = i - pos[i] + 1;
    int j = upper_bound(pr, pr+n, tmp) - pr - 1;
    int target = i - pr[j] + 1;
    if (visit[target] == 0) {
      swap(a[i], a[target]);
      swap(pos[i], pos[target]);
      ans.push_back(make_pair(target, i));
    }
  }
}

int main() {
  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
    b[i].first = a[i];
    b[i].second = i;
  }
  sort(b, b+n);
  for (int i = 0; i < n; i++) pos[b[i].second] = i;
  bool flag = false;
  while (1) {
    if (flag) break;
    int remain = 0;
    fill(visit, visit + n, 0);
    for (int i = 0; i < n; i++) {
      //printf("a[%d] = %d\n", i+1, a[i]);
      if (pos[i] == i) { visit[i] = 2; remain++; continue;}
      if (!visit[i]) {
        dfs(i);
      }
    }
    flag = (remain == n);
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first+1, ans[i].second+1);
  return 0;
}


#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAX=(int)1e5+4;
bool ispr[MAX];
int pr[MAX], pn, a[MAX], pos[MAX];
pair <int, int> b[MAX];
void init() {
  fill(ispr, ispr+MAX, true);
  for (int k = 2; k < MAX; k++) {
    if (ispr[k]) {
      pr[pn++] = k;
    }
    for (int i = 0; i < pn && k * pr[i] < MAX; i++) {
      ispr[k*pr[i]] = false;
      if (k%pr[i] == 0) break;
    }
  }
}

int main() {
  init();
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
    b[i].first = a[i];
    b[i].second = i;
  }
  sort(b, b+n);
  for (int i = 0; i < n; i++) pos[b[i].second] = i;
  bool flag = false;
  vector < pair<int,int> > ans;
  while (1) {
    if (flag) break;
    int remain = 0;
    for (int i = 0; i < n; i++) {
      //printf("a[%d] = %d\n", i+1, a[i]);
      if (pos[i] == i) { remain++; continue;}
      if (pos[i] > i) {
        int tmp = pos[i] - i + 1;
        //printf("tmp: %d\n", tmp);
        int j = upper_bound(pr, pr+pn, tmp) - pr - 1;
        //printf("pr: %d\n", pr[j]);
        int target = i + pr[j] - 1;
        swap(pos[target], pos[i]);
        swap(a[target], a[i]);
        ans.push_back(make_pair(i+1, target+1));
      } else {
        int tmp = i - pos[i] + 1;
        //printf("tmp: %d\n", tmp);
        int j = upper_bound(pr, pr+pn, tmp) - pr - 1;
        //printf("pr: %d\n", pr[j]);
        int target = i - pr[j] + 1;
        swap(pos[target], pos[i]);
        swap(a[target], a[i]);
        ans.push_back(make_pair(target+1, i+1));
      }
    }
    flag = (remain == n);
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first, ans[i].second);
  return 0;
}


#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAX=(int)1e7+4;
bool ispr[MAX];
int pr[MAX], pn, a[MAX], pos[MAX], n;
vector < pair<int,int> > ans;
pair <int, int> b[MAX];
bool visit[MAX];
void init() {
  fill(ispr, ispr+MAX, true);
  for (int k = 2; k < MAX; k++) {
    if (ispr[k]) {
      pr[pn++] = k;
    }
    for (int i = 0; i < pn && k * pr[i] < MAX; i++) {
      ispr[k*pr[i]] = false;
      if (k%pr[i] == 0) break;
    }
  }
}
int dfs(int i) {
  visit[i] = 1;
  int ret = i;
  if (pos[i] > i) {
    int tmp = pos[i] - i + 1;
    int j = upper_bound(pr, pr+n, tmp) -pr - 1;
    int target = i + pr[j] - 1;
    if (visit[target] == 0) {
      ret = dfs(target);
      swap(a[i], a[target]);
      swap(pos[i], pos[target]);
      ans.push_back(make_pair(i, target));
    }
  } else {
    int tmp = i - pos[i] + 1;
    int j = upper_bound(pr, pr+n, tmp) -pr - 1;
    int target = i - pr[j] + 1;
    if (visit[target] == 0) {
      ret = dfs(target);
      swap(a[i], a[target]);
      swap(pos[i], pos[target]);
      ans.push_back(make_pair(target, i));
    }
  }
  return ret;
}

int main() {
  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
    b[i].first = a[i];
    b[i].second = i;
  }
  sort(b, b+n);
  for (int i = 0; i < n; i++) pos[b[i].second] = i;
  bool flag = false;
  while (1) {
    if (flag) break;
    int remain = 0;
    fill(visit, visit + n, 0);
    for (int i = 0; i < n; i++) {
      //printf("a[%d] = %d\n", i+1, a[i]);
      if (pos[i] == i) { visit[i] = 2; remain++; continue;}
      if (!visit[i]) dfs(i);
    }
    flag = (remain == n);
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first+1, ans[i].second+1);
  return 0;
}


#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAX=(int)1e7+4;
bool ispr[MAX];
int pr[MAX], pn, a[MAX], pos[MAX];
pair <int, int> b[MAX];
void init() {
  fill(ispr, ispr+MAX, true);
  for (int k = 2; k < MAX; k++) {
    if (ispr[k]) {
      pr[pn++] = k;
    }
    for (int i = 0; i < pn && k * pr[i] < MAX; i++) {
      ispr[k*pr[i]] = false;
      if (k%pr[i] == 0) break;
    }
  }
}

int main() {
  init();
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
    b[i].first = a[i];
    b[i].second = i;
  }
  sort(b, b+n);
  for (int i = 0; i < n; i++) pos[b[i].second] = i;
  bool flag = false;
  vector < pair<int,int> > ans;
  while (1) {
    if (flag) break;
    int remain = 0;
    for (int i = 0; i < n; i++) {
      //printf("a[%d] = %d\n", i+1, a[i]);
      if (pos[i] == i) { remain++; continue;}
      if (pos[i] > i) {
        int tmp = pos[i] - i + 1;
        //printf("tmp: %d\n", tmp);
        int j = upper_bound(pr, pr+pn, tmp) - pr - 1;
        //printf("pr: %d\n", pr[j]);
        int target = i + pr[j] - 1;
        swap(pos[target], pos[i]);
        swap(a[target], a[i]);
        ans.push_back(make_pair(i+1, target+1));
      } else {
        int tmp = i - pos[i] + 1;
        //printf("tmp: %d\n", tmp);
        int j = upper_bound(pr, pr+pn, tmp) - pr - 1;
        //printf("pr: %d\n", pr[j]);
        int target = i - pr[j] + 1;
        swap(pos[target], pos[i]);
        swap(a[target], a[i]);
        ans.push_back(make_pair(target+1, i+1));
      }
    }
    flag = (remain == n);
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first, ans[i].second);
  return 0;
}


#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAX=(int)1e7+4;
bool ispr[MAX];
int pr[MAX], pn, a[MAX], pos[MAX], n;
vector < pair<int,int> > ans;
pair <int, int> b[MAX];
bool visit[MAX];
void init() {
  fill(ispr, ispr+MAX, true);
  for (int k = 2; k < MAX; k++) {
    if (ispr[k]) {
      pr[pn++] = k;
    }
    for (int i = 0; i < pn && k * pr[i] < MAX; i++) {
      ispr[k*pr[i]] = false;
      if (k%pr[i] == 0) break;
    }
  }
}
void dfs(int i) {
  visit[i] = 1;
  if (pos[i] > i) {
    int tmp = pos[i] - i + 1;
    int j = upper_bound(pr, pr+n, tmp) - pr - 1;
    int target = i + pr[j] - 1;
    if (visit[target] == 0) {
      swap(a[i], a[target]);
      swap(pos[i], pos[target]);
      ans.push_back(make_pair(i, target));
    }
  } else {
    int tmp = i - pos[i] + 1;
    int j = upper_bound(pr, pr+n, tmp) - pr - 1;
    int target = i - pr[j] + 1;
    if (visit[target] == 0) {
      swap(a[i], a[target]);
      swap(pos[i], pos[target]);
      ans.push_back(make_pair(target, i));
    }
  }
}

int main() {
  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
    b[i].first = a[i];
    b[i].second = i;
  }
  sort(b, b+n);
  for (int i = 0; i < n; i++) pos[b[i].second] = i;
  bool flag = false;
  while (1) {
    if (flag) break;
    int remain = 0;
    fill(visit, visit + n, 0);
    for (int i = 0; i < n; i++) {
      //printf("a[%d] = %d\n", i+1, a[i]);
      if (pos[i] == i) { visit[i] = 2; remain++; continue;}
      if (!visit[i]) {
        dfs(i);
      }
    }
    flag = (remain == n);
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first+1, ans[i].second+1);
  return 0;
}


//============================================================================
// Name        : main.cpp
// Author      : discover
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstring>
#include <cstdio>

using namespace std;
#define _IO_ ios::sync_with_stdio(false)
const int N = 112345;

int n;
int a[N];
int b[N];
int p[N], np;
bool vis[N];

void get_prime() {
    memset(vis, 0, sizeof(vis));
    np = 0;
    for (int i = 2; i < N; ++i) {
        if (!vis[i])
            p[np++] = i;
        for (int j = 0, t; j < np && (t = p[j] * i) < N; ++j) {
            vis[t] = 1;
            if (i % p[j] == 0)
                break;

        }
    }
}

vector<int> ans;

void add(int i, int j) {
    ans.push_back(i);
    ans.push_back(j);
    swap(a[i], a[j]);
    b[a[i]] = i;
    b[a[j]] = j;
}

void go(int x, int y) {
    if (x == y)
        return;
    int t = abs(x - y) + 1;
    if (!vis[t])
        add(x, y);
    else {
        if (t == 4) {
            add(y - 2, y);
            add(x, y - 2);
        } else if (t % 2 == 0) {
            add(y - 3, y);
            go(x, y - 3);
        } else {
            for (int i = 1; i < np; ++i) {
                if (!vis[i] && !vis[t + 1 - i]) {
                    add(i + x - 1, y);
                    add(x, i + x - 1);
                    return;
                }
            }
        }
    }
}

int main() {
    _IO_;
    get_prime();
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i], b[a[i]] = i;
    for (int i = 1; i <= n; ++i) {
        go(i, b[i]);
    }
    cout << ans.size() / 2 << endl;
    for (size_t i = 0; i < ans.size(); i += 2) {
        cout << ans[i] << " " << ans[i+1] << "\n";
    }
}


//============================================================================
// Name        : main.cpp
// Author      : discover
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstring>
#include <cstdio>

using namespace std;
#define _IO_ ios::sync_with_stdio(false)
const int N = 112345;

int n;
int a[N];
int b[N];
int p[N], np;
bool vis[N];

void get_prime() {
    memset(vis, 0, sizeof(vis));
    np = 0;
    for (int i = 2; i < N; ++i) {
        if (!vis[i])
            p[np++] = i;
        for (int j = 0, t; j < np && (t = p[j] * i) < N; ++j) {
            vis[t] = 1;
            if (i % p[j] == 0)
                break;

        }
    }
}

vector<int> ans;

void add(int i, int j) {
    ans.push_back(i);
    ans.push_back(j);
    swap(a[i], a[j]);
    b[a[i]] = i;
    b[a[j]] = j;
}

void go(int x, int y) {
    if (x == y)
        return;
    int t = abs(x - y) + 1;
    if (!vis[t])
        add(x, y);
    else {
        if (t == 4) {
            add(y - 2, y);
            add(x, y - 2);
        } else if (t % 2 == 0) {
            add(y - 1, y);
            go(x, y - 1);
        } else {
            for (int i = 1; i < np; ++i) {
                if (!vis[i] && !vis[t + 1 - i]) {
                    add(i + x - 1, y);
                    add(x, i + x - 1);
                    return;
                }
            }
        }
    }
}

int main() {
    _IO_;
    get_prime();
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i], b[a[i]] = i;
    for (int i = 1; i <= n; ++i) {
        go(i, b[i]);
    }
    cout << ans.size() / 2 << endl;
    for (size_t i = 0; i < ans.size(); i += 2) {
        cout << ans[i] << " " << ans[i+1] << "\n";
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	if (n == 10) {
		printf("%d\n", can.size());
		for (auto x: can) {
			printf("%d ", x);
		}
		puts("");
	}
	for (int i = n; i >= 1; i--) {
		if (n == 10) {
			// if (i == 9) {
			for (int j = 1; j <= i; j++) {
				printf("%d%s", a[j], j == i ? "\n" : " ");
			}
			// 	break;
			// }
			if (i == 8) {
				break;
			}
			// if (i == 10) {
			// 	printf("%d %d\n", i, b[i]);
			// }
		}
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cassert>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	// if (n == 10) {
	// 	printf("%d\n", can.size());
	// 	for (auto x: can) {
	// 		printf("%d ", x);
	// 	}
	// 	puts("");
	// }
	for (int i = n; i >= 1; i--) {
		// if (n == 10) {
			// if (i == 9) {
			// for (int j = 1; j <= i; j++) {
			// 	printf("%d%s", a[j], j == i ? "\n" : " ");
			// }
			// 	break;
			// }
			// if (i == 9) {
			// 	break;
			// }
			// if (i == 10) {
			// 	printf("%d %d\n", i, b[i]);
			// }
		// }
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = --upper_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
			// if (*it == 0) {
			// 	for (auto x: can) {
			// 		printf("%d ", x);
			// 	}
			// 	printf("\n%d\n", dif);
			// 	return 0;
			// }
		}
		if (dif != 0) {
			it = --upper_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
			assert(*it != 0);
		}
		// if (i == 10) {
		// 	for (auto x: op) {
		// 		printf("%d ", x);
		// 	}
		// 	printf("\n%d\n", dif);
		// 	return 0;
		// }
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	// printf("%d\n", can.size());
	// for (auto x: can) {
	// 	printf("%d ", x);
	// }
	// puts("");
	for (int i = n; i >= 1; i--) {
		// if (i == 9) {
			for (int j = 1; j <= i; j++) {
				printf("%d%s", a[j], j == i ? "\n" : " ");
			}
		// 	break;
		// }
		if (i == 5) {
			break;
		}
		// if (i == 10) {
		// 	printf("%d %d\n", i, b[i]);
		// }
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	if (n == 10) {
		printf("%d\n", can.size());
		for (auto x: can) {
			printf("%d ", x);
		}
		puts("");
	}
	for (int i = n; i >= 1; i--) {
		if (n == 10) {
			// if (i == 9) {
			for (int j = 1; j <= i; j++) {
				printf("%d%s", a[j], j == i ? "\n" : " ");
			}
			// 	break;
			// }
			if (i == 9) {
				break;
			}
			// if (i == 10) {
			// 	printf("%d %d\n", i, b[i]);
			// }
		}
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		if (i == 10) {
			for (auto x: op) {
				printf("%d ", x);
			}
			return 0;
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		sieve[i] = true;
		for (int j = i+i; j <= n; j++) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 2; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s;
	stringstream ss;
	for (int i = n; i >= 1; i--) {
		if (a[i] == i) continue;
		int dif = i-a[i];
		// if (i == 3) {
		// 	printf("dif: %d\n", dif);
		// }
		bool is_odd = false;
		if (dif % 2) {
			// printf("dif: %d %d\n", i, dif);
			is_odd = true;
			dif--;
		}
		vector<int> op;
		auto it = lower_bound(can.begin(), can.end(), dif);
		op.push_back(*it);
		dif -= *it;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			for (auto x: can) {
				if (sieve[dif-x+1]) {
					op.push_back(x);
					op.push_back(dif-x);
					break;
				}
			}
		}
		if (is_odd) {
			// printf("%d\n", i);
			op.push_back(1);
		}
		int y = a[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	if (n == 10) {
		printf("%d\n", can.size());
		for (auto x: can) {
			printf("%d ", x);
		}
		puts("");
	}
	for (int i = n; i >= 1; i--) {
		if (n == 10) {
			// if (i == 9) {
			for (int j = 1; j <= i; j++) {
				printf("%d%s", a[j], j == i ? "\n" : " ");
			}
			// 	break;
			// }
			if (i == 9) {
				break;
			}
			// if (i == 10) {
			// 	printf("%d %d\n", i, b[i]);
			// }
		}
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j++) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	for (int i = n; i >= 1; i--) {
		int dif = i-a[i];
		bool is_odd = false;
		if (dif % 2) {
			is_odd = true;
			dif--;
		}
		vector<int> op;
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		if (is_odd) {
			// printf("%d\n", i);
			op.push_back(1);
		}
		int y = a[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j++) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s;
	stringstream ss;
	for (int i = n; i >= 1; i--) {
		int dif = i-a[i];
		bool is_odd = false;
		if (dif % 2) {
			is_odd = true;
			dif--;
		}
		vector<int> op;
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			for (auto x: can) {
				if (sieve[dif-x+1]) {
					op.push_back(x);
					op.push_back(dif-x);
					break;
				}
			}
		}
		if (is_odd) {
			// printf("%d\n", i);
			op.push_back(1);
		}
		int y = a[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	if (n == 10) {
		printf("%d\n", can.size());
		for (auto x: can) {
			printf("%d ", x);
		}
		puts("");
	}
	for (int i = n; i >= 1; i--) {
		if (n == 10) {
			// if (i == 9) {
			for (int j = 1; j <= i; j++) {
				printf("%d%s", a[j], j == i ? "\n" : " ");
			}
			// 	break;
			// }
			if (i == 9) {
				break;
			}
			// if (i == 10) {
			// 	printf("%d %d\n", i, b[i]);
			// }
		}
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (i == 10) {
			return 0;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		sieve[i] = true;
		for (int j = i+i; j <= n; j++) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 2; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s;
	stringstream ss;
	for (int i = n; i >= 1; i--) {
		int dif = i-a[i];
		bool is_odd = false;
		if (dif % 2) {
			is_odd = true;
			dif--;
		}
		vector<int> op;
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			for (auto x: can) {
				if (sieve[dif-x+1]) {
					op.push_back(x);
					op.push_back(dif-x);
					break;
				}
			}
		}
		if (is_odd) {
			// printf("%d\n", i);
			op.push_back(1);
		}
		int y = a[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	// printf("%d\n", can.size());
	// for (auto x: can) {
	// 	printf("%d ", x);
	// }
	// puts("");
	for (int i = n; i >= 1; i--) {
		// if (i == 9) {
		// 	for (int j = 1; j <= i; j++) {
		// 		printf("%d%s", a[j], j == i ? "\n" : " ");
		// 	}
		// 	break;
		// }
		// if (i == 10) {
		// 	printf("%d %d\n", i, b[i]);
		// }
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	// printf("%d\n", can.size());
	// for (auto x: can) {
	// 	printf("%d ", x);
	// }
	// puts("");
	for (int i = n; i >= 1; i--) {
		if (n == 10) {
			// if (i == 9) {
			for (int j = 1; j <= i; j++) {
				printf("%d%s", a[j], j == i ? "\n" : " ");
			}
			// 	break;
			// }
			if (i == 5) {
				break;
			}
			// if (i == 10) {
			// 	printf("%d %d\n", i, b[i]);
			// }
		}
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	if (n == 10) {
		printf("%d\n", can.size());
		for (auto x: can) {
			printf("%d ", x);
		}
		puts("");
		return 0;
	}
	for (int i = n; i >= 1; i--) {
		if (n == 10) {
			// if (i == 9) {
			for (int j = 1; j <= i; j++) {
				printf("%d%s", a[j], j == i ? "\n" : " ");
			}
			// 	break;
			// }
			if (i == 5) {
				break;
			}
			// if (i == 10) {
			// 	printf("%d %d\n", i, b[i]);
			// }
		}
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j++) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	for (int i = n; i >= 1; i--) {
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(a[y], a[y+x]);
			swap(b[a[y]], b[a[y+x]]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	// ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n+1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<int> b(n+1);
	for (int i = 1; i <= n; i++) {
		b[a[i]] = i;
	}
	vector<bool> sieve(n+1, true);
	sieve[0] = sieve[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!sieve[i]) continue;
		for (int j = i+i; j <= n; j += i) {
			sieve[j] = false;
		}
	}
	vector<int> can;
	for (int i = 3; i <= n; i++) {
		if (sieve[i]) {
			can.push_back(i-1);
		}
	}
	int ans = 0;
	string s = "";
	stringstream ss;
	if (n == 10) {
		printf("%d\n", can.size());
		for (auto x: can) {
			printf("%d ", x);
		}
		puts("");
	}
	for (int i = n; i >= 1; i--) {
		if (n == 10) {
			// if (i == 9) {
			for (int j = 1; j <= i; j++) {
				printf("%d%s", a[j], j == i ? "\n" : " ");
			}
			// 	break;
			// }
			if (i == 4) {
				break;
			}
			// if (i == 10) {
			// 	printf("%d %d\n", i, b[i]);
			// }
		}
		int dif = i-b[i];
		vector<int> op;
		if (dif % 2) {
			op.push_back(1);
			dif--;
		}
		auto it = can.end()-1;
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			it = lower_bound(can.begin(), it+1, dif);
			op.push_back(*it);
			dif -= *it;
		}
		if (dif != 0) {
			if (dif == 2) {
				op.push_back(2);
			}
			else {
				for (auto x: can) {
					if (sieve[dif-x+1]) {
						op.push_back(x);
						op.push_back(dif-x);
						break;
					}
				}
			}
		}
		int y = b[i];
		for (auto x: op) {
			// printf("%d\n", i);
			swap(b[a[y]], b[a[y+x]]);
			swap(a[y], a[y+x]);
			// printf("%d %d\n", y, y+x);
			ss << y << " " << y+x << '\n';
			ans++;
			y += x;
		}
	}
	ss << 'a';
	getline(ss, s, 'a');
	printf("%d\n", ans);
	printf("%s\n", s.c_str());
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 100010;
vector<int> p;
vector<bool> isprime(N, true);

void initPrime(int n) {
    for (int i = 2; i < n; ++i) {
        if (isprime[i]) p.push_back(i);
        for (auto& pr : p) {
            if (pr * i >= n) break;
            isprime[pr * i] = false;
            if (i % pr == 0) break;
        }
    }
}

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

    vector< pair<int, int> > pa(n + 1, make_pair(-1, -1));
    for (auto p1 : p) for (auto p2 : p) {
        if (p1 + p2 <= n && pa[p1 + p2].first == -1) pa[p1 + p2] = make_pair(p1, p2);
    }

    vector< pair<int, int> > steps;
    vector<int> pos(n + 1, -1);
    for (int i = 1; i <= n; ++i) pos[a[i]] = i;
    for (int i = 1; i <= n; ++i) {
        assert(pos[i] >= i);

        auto swap_pos = [&a, &pos, &steps](int i, int j) {
            assert(i < j);
            swap(a[i], a[j]);
            pos[a[i]] = i;
            pos[a[j]] = j;
            steps.push_back(make_pair(i, j));
        };

        if (pos[i] == i) {
            continue;
        }

        if (isprime[pos[i] - i + 1]) {
            swap_pos(i, pos[i]);
            continue;
        }
        int dist = pos[i] - i + 1;
        int l = pa[dist].first, r = pa[dist].second;

//        printf("i = %d, dist = %d, l = %d, r = %d\n", i, pos[i] - i + 1, l, r);
//        printf("i = %d, dist = %d\n", i, dist);

        int s2 = pos[i] - r + 1, s1 = pos[i] - r + 1 - l + 1;
        swap_pos(s2, pos[i]);
        swap_pos(s1, s2);
        swap_pos(i, s1);
    }

    printf("%d\n", steps.size());
    for (auto& t : steps) {
        printf("%d %d\n", t.first, t.second);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 100010;
vector<int> p;
vector<bool> isprime(N, true);

void initPrime(int n) {
    for (int i = 2; i < n; ++i) {
        if (isprime[i]) p.push_back(i);
        for (auto& pr : p) {
            if (pr * i >= n) break;
            isprime[pr * i] = false;
            if (i % pr == 0) break;
        }
    }
}

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

    initPrime(n + 1);

    vector< pair<int, int> > pa(n + 1, make_pair(-1, -1));
    for (auto p1 : p) for (auto p2 : p) {
        if (p1 + p2 <= n && pa[p1 + p2].first == -1) pa[p1 + p2] = make_pair(p1, p2);
    }

    vector< pair<int, int> > steps;
    vector<int> pos(n + 1, -1);
    for (int i = 1; i <= n; ++i) pos[a[i]] = i;
    for (int i = 1; i <= n; ++i) {
        assert(pos[i] >= i);

        auto swap_pos = [&a, &pos, &steps](int i, int j) {
            assert(i < j);
            swap(a[i], a[j]);
            pos[a[i]] = i;
            pos[a[j]] = j;
            steps.push_back(make_pair(i, j));
        };

        if (pos[i] == i) {
            continue;
        }

        int dist = pos[i] - i + 1;
        if (isprime[dist]) {
            swap_pos(i, pos[i]);
            continue;
        }
        dist -= (dist&1);
         int l = pa[dist].first, r = pa[dist].second;

        int s2 = pos[i] - r + 1, s1 = pos[i] - r + 1 - l + 1;
        swap_pos(s2, pos[i]);
        swap_pos(s1, s2);
        swap_pos(i, s1);
    }

    printf("%d\n", steps.size());
    for (auto& t : steps) {
        printf("%d %d\n", t.first, t.second);
    }

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <sstream>
#include <vector>
#include <complex>
#include <ctime>
#include <stack>

//#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector< VI > VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define REP(i, n) for(int i = 0; i < (n); ++i)
#define RREP(i, n) for(int i = (n) - 1; i >= 0; --i)
#define FOR(i, x, y) for(int i = (x); i <= (y); ++i)
#define RFOR(i, x, y) for(int i = (x); i >= (y); --i)
#define SZ(a) (ll)(a).size()
#define ALL(a) (a).begin(),(a).end()
#define SORT(a) sort(ALL(a))
#define CLEAR(x) memset(x, 0, sizeof x);
#define COPY(FROM, TO) memcpy(TO, FROM, sizeof TO);
#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
#define pb push_back
#define mk make_pair
#define sqr(x) ((x)*(x))
#define X first
#define Y second
//typedef tree<int, null_type, less<int>,
//  rb_tree_tag, tree_order_statistics_node_update> order_set;

const long double pi=acos(-1.0);
const double EPS = 1E-9;
const int INF = 2 * (1e+9) + 1;
const int NMAX = 100000;
const int MOD = (1e+9) + 7;

int n, k;
int a[100100], b[100100];
set<int> pr;
map<int, int> mp, ord;
int main () {
  vector<char> prime (NMAX+1, true);
  prime[0] = prime[1] = false;
  for (int i=2; i<=NMAX; ++i)
  if (prime[i])
      if (i * 1ll * i <= NMAX)
          for (int j=i*i; j<=NMAX; j+=i)
              prime[j] = false;
  REP (i, NMAX + 1) {
    if (prime[i]) pr.insert(-i);
  }
  cin >> n;
  REP (i, n) {
    cin >> a[i];
    b[i] = a[i];
    mp[a[i]] = i;
  }
  sort (b, b + n);
  REP (i, n) {
    ord[i] = b[i];
  }
  VPII ans;
  REP (i, n) {
    int pos = mp[ord[i]];
    while (pos != i) {
      int len = pos - i + 1;
      int cut = -(*pr.lower_bound(-len));
      int new_pos = pos - cut + 1;
      ans.pb(mk(new_pos, pos));
      mp[a[new_pos]] = pos;
      mp[a[pos]] = new_pos;
      swap (a[pos], a[new_pos]);
      pos = new_pos;
    }
  }
  cout << SZ(ans) << endl;
  REP (i, SZ(ans)) {
    cout << ans[i].X + 1 << " " << ans[i].Y + 1 << endl;
  }
}
/*
4
4 2 3 1
*/


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<deque>

using namespace std;

typedef long long ll;
typedef long double ld;

#define Name ""

const ld eps=1e-9;
const int maxn=2e5;
const ll inf=1e9+7;

vector<int> num;

vector<pair<int,int> > an;

typedef vector<int>::iterator vii;

int main()
{

    for (int i=2;i<100005;i++)
    {
        bool p=true;
        for (int j=0;j<num.size();j++)
        {
            if (i%num[j]==0)
            {
                p=false;
                break;
            }
        }
        if (p)
        {
            num.push_back(i);
        }
    }
    vector<int> p,pos;
    int n;

    scanf("%d",&n);
    p.resize(n);
    pos.resize(n);
    for (int i=0;i<n;i++)
    {
        scanf("%d",&p[i]);
        p[i]--;
        pos[p[i]]=i;
    }
    for (int i=0;i<n;i++)
    {
        while (p[i]!=i)
        {
          //  cout<<pos[i]-i+1<<endl;
            vii j=upper_bound(num.begin(),num.end(),pos[i]-i+1);
            j--;
            int cnum=*j;
         //   cout<<cnum<<endl;
        //    cout<<pos[i]<<' '<<pos[i]-cnum+1<<endl;
            an.push_back(make_pair(pos[i]-cnum+2,pos[i]+1));
            int t1,t2;

            p[pos[i]]=p[pos[i]-cnum+1];
            p[pos[i]-cnum+1]=i;
            pos[p[pos[i]]]=pos[i];
            pos[i]-=(cnum-1);

            p[pos[i]]=i;

        }
    }
    printf("%d\n",an.size());
    for (int i=0;i<an.size();i++)
    {
        printf("%d %d\n",an[i].first,an[i].second);
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <random>
#include <set>
#include <sstream>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
const int N = 200000;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;

	vector<bool> p(N, true);
	vector<int> prime;
	p[0] = p[1] = false;
	for(int i=2;i<N;++i)
		if(p[i]){
			prime.push_back(i);
			for(int j=i+i;j<N;j+=i)
				p[j]=false;
		}

	vector<int> t(n), pos(n);
	for(int i=0;i< n;++i){
		cin >> t[i];
		--t[i];
		pos[t[i]] = i;
	}
	vector<pi> res;
	for(int i=0;i<n;++i)
		while(pos[i]!=i){
			int p = 0;
			while(pos[i]-i+1>=prime[p])
				++p;
			--p;
			//swap pos, pos-p+1
			int p1 = pos[i], p2=pos[i]-prime[p]+1;
			res.push_back(pi{p1, p2});
			pos[t[p2]] = p1;
			pos[i] = p2;
			swap(t[p1], t[p2]);
		}

	cout << res.size() << '\n';
	for(auto& p:res)
		cout << p.first+1 << ' ' << p.second+1 << '\n';
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <random>
#include <set>
#include <sstream>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
const int N = 200000;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;

	vector<bool> p(N, true);
	vector<int> prime;
	p[0] = p[1] = false;
	for(int i=2;i<N;++i)
		if(p[i]){
			prime.push_back(i);
			for(int j=i+i;j<N;j+=i)
				p[j]=false;
		}

	vector<int> t(n), pos(n);
	for(int i=0;i< n;++i){
		cin >> t[i];
		--t[i];
		pos[t[i]] = i;
	}
	vector<pi> res;
	for(int i=0;i<n;++i)
		while(pos[i]!=i){
			int p = 0;
			while(pos[i]-i+1>=prime[p])
				++p;
			--p;
			//swap pos, pos-p+1
			int p1 = pos[i], p2=pos[i]-prime[p]+1;
			res.push_back(pi{p1, p2});
			pos[t[p2]] = p1;
			pos[i] = p2;
			swap(t[p1], t[p2]);
		}

	cout << res.size() << '\n';
	for(auto& p:res)
		cout << p.second+1 << ' ' << p.first+1 << '\n';
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#define Mod 100000000
#define EPS 10e-9
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
typedef long long LL;

const int N=100005;
int a[N],from[N];
int p[N],prime[N];
int step[5*N][2];

int main()
{
   int n;
   int i,j;
   scanf("%d",&n);
   int t=0;
   for(int i=2;i<N;i++)
		if(!p[i])
		{
			for(int j=i*2;j<N;j+=i) p[j]=1;
			prime[t++]=i;
		}
   for(i=1;i<=n;i++)
   {
      scanf("%d",&a[i]);
      from[a[i]]=i;
   }
   int l=0;
   for(i=1;i<=n;i++)
   {
      int d=from[i]-i+1;
      int pre=from[i];
      if(d==1)
         continue;
      for(j=t-1;j>=0;j--)
      {
          if(d>=prime[j])
          {
              from[a[pre]]=pre-prime[j]+1;
              from[a[pre-prime[j]+1]]=pre;
              step[l][0]=pre-prime[j]+1; step[l][1]=pre; l++;
              swap(a[pre],a[pre-prime[j]+1]);
              d=d-prime[j]+1;
              pre=pre-prime[j]+1;
          }
          if(d==0)
             break;
      }
   }
   printf("%d\n",l);
   for(i=0;i<l;i++)
   printf("%d %d\n",step[i][0],step[i][1]);
   return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#define Mod 100000000
#define EPS 10e-9
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
typedef long long LL;

const int N=100005;
int a[N],from[N];
int p[N],prime[N];
int step[5*N][2];

int main()
{
   int n;
   int i,j;
   scanf("%d",&n);
   int t=0;
   for(int i=2;i<N;i++)
		if(!p[i])
		{
			for(int j=i*2;j<N;j++) p[j]=1;
			prime[t++]=i;
		}
   for(i=1;i<=n;i++)
   {
      scanf("%d",&a[i]);
      from[a[i]]=i;
   }
   int l=0;
   for(i=1;i<=n;i++)
   {
      int d=from[i]-i+1;
      int pre=from[i];
      if(d==1)
         continue;
      for(j=t-1;j>=0;j--)
      {
          if(d>=prime[j])
          {
              from[a[pre]]=pre-prime[j]+1;
              from[a[pre-prime[j]+1]]=pre;
              step[l][0]=pre-prime[j]+1; step[l][1]=pre; l++;
              swap(a[pre],a[pre-prime[j]+1]);
              d=d-prime[j]+1;
              pre=pre-prime[j]+1;
          }
          if(d==0)
             break;
      }
   }
   printf("%d\n",l);
   for(i=0;i<l;i++)
   printf("%d %d\n",step[i][0],step[i][1]);
   return 0;
}


#include <cstdio>
#include <cassert>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;

int p[100005];
pair<int, int> a[100005];
bool isPrime[100005];

void sw(int i, int j){
	swap(p[a[i].first], p[a[j].first]);
	swap(a[i], a[j]);
	/*int I=a[i].first;
	int J=a[j].first;
	swap(p[I], p[J]);
	swap(a[i], a[j]);*/
}

int main(){
	for(int i=2; i < 100005; ++i){
		isPrime[i]=1;
		for(int j=2; j*j <= i; ++j){
			if(i%j == 0)
				isPrime[i]=0;
		}
	}
	int n;
	scanf("%d", &n);
	for(int i=0; i < n; ++i){
		int b;
		scanf("%d", &b);
		--b;
		a[i]=make_pair(b, i);
	}
	for(int i=0; i < n; ++i){
		p[a[i].first]=i;
	}
	vector<pair<int, int> > ans;
	for(int i=0; i < n; ++i){
		while(p[i] != i){
			int j;
			for(j=p[i]-i+1; !isPrime[j]; --j);
			ans.push_back(make_pair(p[i]-j+1, p[i]));
			sw(p[i], p[i]-j+1);
		}
	}
	assert(ans.size() <= 5*n);
	printf("%d\n", ans.size());
	for(int i=0; i < ans.size(); ++i)
		printf("%d %d\n", ans[i].first+1, ans[i].second+1);
	return 0;
}


#include <cstdio>
#include <cassert>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;

int p[100005];
pair<int, int> a[100005];
bool isPrime[100005];

void sw(int i, int j){
	int I=a[i].second;
	int J=a[j].second;
	swap(p[I], p[J]);
	swap(a[i], a[j]);
}

int main(){
	for(int i=2; i < 100005; ++i){
		isPrime[i]=1;
		for(int j=2; j*j <= i; ++j){
			if(i%j == 0)
				isPrime[i]=0;
		}
	}
	int n;
	scanf("%d", &n);
	for(int i=0; i < n; ++i){
		int b;
		scanf("%d", &b);
		a[i]=make_pair(b, i);
	}
	sort(a, a+n);
	for(int i=0; i < n; ++i){
		p[a[i].second]=i;
	}
	vector<pair<int, int> > ans;
	for(int i=0; i < n; ++i){
		while(p[i] != i){
			int j;
			for(j=p[i]-i+1; !isPrime[j]; --j);
			ans.push_back(make_pair(p[i]-j+1, p[i]));
			sw(p[i], p[i]-j+1);
		}
	}
	assert(ans.size() <= 5*n);
	printf("%d\n", ans.size());
	for(int i=0; i < ans.size(); ++i)
		printf("%d %d\n", ans[i].first+1, ans[i].second+1);
	return 0;
}


#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;

int p[100005];
pair<int, int> a[100005];
bool isPrime[100005];

void sw(int i, int j){
	int I=a[i].second;
	int J=a[j].second;
	swap(p[I], p[J]);
	a[i].second=J;
	a[j].second=I;
}

int main(){
	for(int i=2; i < 100005; ++i){
		isPrime[i]=1;
		for(int j=2; j*j <= i; ++j){
			if(i%j == 0)
				isPrime[i]=0;
		}
	}
	int n;
	scanf("%d", &n);
	for(int i=0; i < n; ++i){
		int b;
		scanf("%d", &b);
		a[i]=make_pair(b, i);
	}
	sort(a, a+n);
	for(int i=0; i < n; ++i){
		p[a[i].second]=i;
	}
	vector<pair<int, int> > ans;
	for(int i=0; i < n; ++i){
		while(p[i] != i){
			int j;
			for(j=p[i]-i+1; !isPrime[j]; --j);
			ans.push_back(make_pair(p[i]-j+1, p[i]));
			sw(p[i], p[i]-j+1);
		}
	}
	printf("%d\n", ans.size());
	for(int i=0; i < ans.size(); ++i)
		printf("%d %d\n", ans[i].first+1, ans[i].second+1);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

bool notPrime[100001];
vector <int> unit;

int n;
int data[100000], loc[100000];

int dp[100001], _next[100001];

int getAns(int cur)
{
	int &ret = dp[cur];
	if(ret != -1) return ret;

	if(cur == 0) ret = 0;
	else
	{
		ret = cur + 1;
		for(int v: unit)
		{
			if(v > cur) break;
			int temp = getAns(cur - v) + 1;
			if(temp < ret)
			{
				ret = temp;
				_next[cur] = cur - v;
			}
		}
	}

	return ret;
}

int main(void)
{
	for(int i = 2; i < 100000; i++)
	{
		if(notPrime[i] == false)
		{
			unit.push_back(i - 1);
			for(int j = i * 2; j < 100000; j += i)
				notPrime[j] = true;
		}
	}

	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d", data + i);
		data[i]--;
		loc[data[i]] = i;
	}

	memset(dp, -1, sizeof(dp));

	vector <pair<int, int>> ans;
	for(int i = 0; i < n; i++)
	{
		int curLoc = loc[i];
		int gap = curLoc - i;

		while(gap > 0)
		{
			getAns(gap);
			int _nextGap = _next[gap];
			int &u = data[i + _nextGap], &v = data[i + gap];
			ans.push_back(make_pair(i + _nextGap + 1, i + gap + 1));
			swap(loc[u], loc[v]);
			swap(u, v);
			gap = _nextGap;
		}
	}

	printf("%d\n", (int)ans.size());
	for(auto v: ans) printf("%d %d\n", v.first, v.second);

	return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

bool notPrime[100001];
vector <int> unit;

int n;
int data[100000], loc[100000];

int dp[100001], _next[100001];

int getAns(int cur)
{
	int &ret = dp[cur];
	if(ret != -1) return ret;

	if(cur == 0) ret = 0;
	else
	{
		if(notPrime[cur] == false)
		{
			ret = 1;
			_next[cur] = 0;
		}
		else
		{
			ret = cur + 1;
			for(int v: unit)
			{
				if(v > cur) break;
				int temp = getAns(cur - v) + 1;
				if(temp < ret)
				{
					ret = temp;
					_next[cur] = cur - v;
				}
			}
		}
	}

	return ret;
}

int main(void)
{
	for(int i = 2; i < 100000; i++)
	{
		if(notPrime[i] == false)
		{
			unit.push_back(i - 1);
			for(int j = i * 2; j < 100000; j += i)
				notPrime[j] = true;
		}
	}

	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d", data + i);
		data[i]--;
		loc[data[i]] = i;
	}

	memset(dp, -1, sizeof(dp));

	vector <pair<int, int>> ans;
	for(int i = 0; i < n; i++)
	{
		int curLoc = loc[i];
		int gap = curLoc - i;

		while(gap > 0)
		{
			getAns(gap);
			int _nextGap = _next[gap];
			int &u = data[i + _nextGap], &v = data[i + gap];
			ans.push_back(make_pair(i + _nextGap + 1, i + gap + 1));
			swap(loc[u], loc[v]);
			swap(u, v);
			gap = _nextGap;
		}
	}

	printf("%d\n", (int)ans.size());
	for(auto v: ans) printf("%d %d\n", v.first, v.second);

	return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

bool notPrime[100001];
vector <int> unit;

int n;
int data[100000], loc[100000];

int dp[100001], _next[100001];

int getAns(int cur)
{
	int &ret = dp[cur];
	if(ret != -1) return ret;

	if(cur == 0) ret = 0;
	else
	{
		if(notPrime[cur] == false)
		{
			ret = 1;
			_next[cur] = 0;
		}
		else
		{
			ret = cur + 1;
			for(int v: unit)
			{
				if(v > cur) break;
				int temp = getAns(cur - v) + 1;
				if(temp < ret)
				{
					ret = temp;
					_next[cur] = cur - v;
				}
			}
		}
	}

	return ret;
}

int main(void)
{
	for(int i = 2; i < 100000; i++)
	{
		if(notPrime[i] == false)
		{
			unit.push_back(i - 1);
			for(int j = i * 2; j < 100000; j += i)
				notPrime[j] = true;
		}
	}

	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d", data + i);
		data[i]--;
		loc[data[i]] = i;
	}

	memset(dp, -1, sizeof(dp));

	vector <pair<int, int>> ans;
	for(int i = 0; i < n; i++)
	{
		int curLoc = loc[i];
		int gap = curLoc - i;

		while(gap > 0)
		{
			getAns(gap);
			int _nextGap = _next[gap];
			int &u = data[i + _nextGap], &v = data[i + gap];
			ans.push_back(make_pair(i + _nextGap + 1, i + gap + 1));
			swap(loc[u], loc[v]);
			swap(u, v);
			gap = _nextGap;
		}
	}

	printf("%d\n", (int)ans.size());
	for(auto v: ans) printf("%d %d\n", v.first, v.second);

	return 0;
}