Problem: To learn as soon as possible the latest news about their favourite fundamentally new operating system, BolgenOS community from Nizhni Tagil decided to develop a scheme. According to this scheme a community member, who is the first to learn the news, calls some other member, the latter, in his turn, calls some third member, and so on; i.e. a person with index i got a person with index fi, to whom he has to call, if he learns the news. With time BolgenOS community members understood that their scheme doesn't work sometimes — there were cases when some members didn't learn the news at all. Now they want to supplement the scheme: they add into the scheme some instructions of type (xi, yi), which mean that person xi has to call person yi as well. What is the minimum amount of instructions that they need to add so, that at the end everyone learns the news, no matter who is the first to learn it?

Input

The first input line contains number n (2 ≤ n ≤ 105) — amount of BolgenOS community members. The second line contains n space-separated integer numbers fi (1 ≤ fi ≤ n, i ≠ fi) — index of a person, to whom calls a person with index i.

Output

In the first line output one number — the minimum amount of instructions to add. Then output one of the possible variants to add these instructions into the scheme, one instruction in each line. If the solution is not unique, output any.

Examples

Input

3
3 3 2


Output

1
3 1


Input

7
2 3 1 3 4 4 1


Output

3
2 5
2 6
3 7

Solution (in PYTHON3):

Solution:
def dfs_1(graph):
    n=len(graph)
    stack=[]
    recursive_stack=[]
    status=[[False,0] for i in range(n)]
    for i in range(n):
        if not status[i][0]:
            recursive_stack.append(i)
            status[i][0]=True
            __dfs(graph,status,stack,recursive_stack)
    return stack            

def __dfs(graph,status,stack,recursive_stack):
    while len(recursive_stack):
        vert=recursive_stack.pop()
       # print(vert)
        if status[vert][1]!=len(graph[vert]):
            act=graph[vert][status[vert][1]]
            status[vert][1]+=1
         #   print(act)
            if not status[act][0]:
                status[act][0]=True
                recursive_stack.append(vert)
                recursive_stack.append(act)    
            else:
                recursive_stack.append(vert)                        
        else:
            stack.append(vert)
           # print("added",vert)        



def reverse_graph(graph):
    n=len(graph)
    new_graph=[[]for i in range(n)]
    pos =0
    for i in graph:
        for j in i:
            new_graph[j].append(pos)
        pos+=1    
    return new_graph

def dfs_2(graph,stack):
    n=len(graph)
    status=[ [False,0] for i in range(n)]
    scc=0
    dag=[-1 for i in range(n)]
    recursive_stack=[]
    while len(stack) > 0:
        root=stack.pop()
        if not status[root][0]:
            __dfs_2(graph,status,scc,dag,root,recursive_stack)
            scc+=1
    return (scc,dag)

def __dfs_2(graph,status,scc,dag,root,recursive_stack):
    status[root][0]=True
    recursive_stack.append(root)
    while len(recursive_stack):
        vert=recursive_stack.pop()
       # print(vert)
        if status[vert][1]!=len(graph[vert]):
            act=graph[vert][status[vert][1]]
            status[vert][1]+=1
           # print(act)
            if not status[act][0]:
                status[act][0]=True
                recursive_stack.append(vert)
                recursive_stack.append(act)    
            else:
                recursive_stack.append(vert)                        
        else:
            dag[vert]=scc
            #stack.append(vert)
           # print("added",vert) 

def SCC(graph):
    stack=dfs_1(graph)
    rvgraph=reverse_graph(graph)
    return dfs_2(rvgraph,stack)
                
def get_dag_degrees(graph):
    n_scc,GSCC= SCC(graph)
    dag=[[]for i in range(n_scc)]
    degres=[[0,0]for i in range(n_scc)]
    count=0
    for i in graph:
        for j in i :
            if GSCC[count]!=GSCC[j]:
                dag[GSCC[count]].append(GSCC[j])
                degres[GSCC[count]][1]+=1
                degres[GSCC[j]][0]+=1     
        count+=1
    return dag,degres,GSCC,n_scc

def part_degrees(degrees):
    in_degree=[]
    cout=0
    for i in degrees:
        if i[0]==0:
            in_degree.append(cout)
        cout+=1    
    return in_degree

def solve(graph):
    dag,degrees,GSCC,n_scc=get_dag_degrees(graph)
    if n_scc==1:
        print(0)
        return
    mapping=[-1 for i in range(len(dag))]
    in_degree=part_degrees(degrees)
    for i in range(len(graph)):
        if mapping[GSCC[i]]==-1:
            mapping[GSCC[i]]=i  
    status=[[False,0] for i in range(len(dag))]
    current_info=[0,-1]
    recursive_stack=[]
    upd_arcs=[]
    last_vert=[-1]
    for i in in_degree:
        if not status[i][0]:
            if last_vert[0] != -1:
                current_info[0]+=1 
                upd_arcs.append((last_vert[0],i))
            __dfs_solve(dag,status,degrees,upd_arcs,i,current_info,last_vert,recursive_stack)
            current_info[1]=-1     
    upd_arcs.append((last_vert[0],in_degree[0]))
    current_info[0]+=1
    print(current_info[0])
    for i in upd_arcs:
        print(mapping[i[0]]+1,mapping[i[1]]+1)
                                
def __dfs_solve(graph,status,degrees,upd_arcs,root,current_info,last_vert,recursive_stack):
    status[root][0]=True
    recursive_stack.append(root)
    while len(recursive_stack):
        vert=recursive_stack.pop()
       # print(vert)
        if status[vert][1]!=len(graph[vert]):
            act=graph[vert][status[vert][1]]
            status[vert][1]+=1
          #  print(act)
            if not status[act][0]:
                status[act][0]=True
                recursive_stack.append(vert)
                recursive_stack.append(act)    
            else:
                recursive_stack.append(vert)                        
        else:
            if degrees[vert][1]==0:
                if current_info[1]!=-1:
                    current_info[0]+=1
                    upd_arcs.append((current_info[1],vert))
                current_info[1]=last_vert[0]=vert
    #if degrees[root][1]==0:
     #   if current_info[1]!=-1:
      #      current_info[0]+=1
       #     upd_arcs.append((current_info[1],root))
       # current_info[1]=last_vert[0]=root                                    

n=int(input())
graph=[[]for i in range(n)]
data_set=input().split()
for i in range(n):
    graph[i].append(int(data_set[i])-1)
    
solve(graph)