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

Solution:
#include <bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
template <typename first, typename second>
ostream& operator<<(ostream& os, const pair<first, second>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ",";
    os << *it;
  }
  return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename first, typename second>
ostream& operator<<(ostream& os, const map<first, second>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
void faltu() { cerr << endl; }
template <typename T>
void faltu(T a[], int n) {
  for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
  cerr << endl;
}
template <typename T, typename... hello>
void faltu(T arg, const hello&... rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
const int mx = 1e5 + 5;
int n, m;
vector<int> adjlist[mx];
vector<int> rev[mx];
vector<int> adjlist2[mx];
vector<int> adjlist3[mx];
int col[mx];
int ans;
int in[mx], out[mx];
vector<int> comp[mx];
vector<pair<int, int> > edge;
bool used[mx];
bool vis[mx];
vector<int> toposort;
void dfs_topo(int u) {
  vis[u] = 1;
  for (auto v : adjlist[u]) {
    if (!vis[v]) dfs_topo(v);
  }
  toposort.push_back(u);
}
void dfs_topo2(int u) {
  vis[u] = 1;
  for (auto v : adjlist2[u]) {
    if (!vis[v]) dfs_topo2(v);
  }
  toposort.push_back(u);
}
void dfs1(int u, int cc) {
  col[u] = cc;
  comp[cc].push_back(u);
  vis[u] = 1;
  for (auto v : rev[u]) {
    if (!vis[v]) dfs1(v, cc);
  }
}
vector<int> vec1[mx], vec2[mx];
void dfs2(int u, int dustho) {
  if (in[u] == 0) vec1[dustho].push_back(comp[u].back());
  if (out[u] == 0) vec2[dustho].push_back(comp[u].back());
  vis[u] = 1;
  for (auto v : adjlist3[u]) {
    if (!vis[v]) dfs2(v, dustho);
  }
}
int main() {
  int T = 1;
  for (int test = 1; test <= T; ++test) {
    scanf("%d", &n);
    ans = 0;
    for (int i = 0; i < n + 2; ++i) {
      adjlist[i].clear();
      rev[i].clear();
      adjlist2[i].clear();
      col[i] = 0;
      vis[i] = 0;
    }
    for (int i = 1; i <= n; ++i) {
      int u, v;
      u = i;
      scanf("%d", &v);
      adjlist[u].push_back(v);
      rev[v].push_back(u);
    }
    toposort.clear();
    for (int i = 1; i <= n; ++i) {
      if (!vis[i]) dfs_topo(i);
    }
    reverse(toposort.begin(), toposort.end());
    memset(vis, 0, sizeof(vis));
    int nxt = 1;
    for (auto u : toposort) {
      if (!vis[u]) {
        dfs1(u, nxt++);
      }
    }
    for (int i = 1; i <= n; ++i) {
      for (auto v : adjlist[i]) {
        if (col[i] != col[v]) {
          adjlist2[col[i]].push_back(col[v]);
          adjlist3[col[i]].push_back(col[v]);
          adjlist3[col[v]].push_back(col[i]);
        }
      }
    }
    for (int i = 1; i < nxt; ++i) {
      sort(adjlist2[i].begin(), adjlist2[i].end());
      adjlist2[i].erase(unique(adjlist2[i].begin(), adjlist2[i].end()),
                        adjlist2[i].end());
    }
    for (int i = 1; i < nxt; ++i) {
      sort(adjlist3[i].begin(), adjlist3[i].end());
      adjlist3[i].erase(unique(adjlist3[i].begin(), adjlist3[i].end()),
                        adjlist3[i].end());
    }
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    for (int i = 1; i < nxt; ++i) {
      for (auto v : adjlist2[i]) {
        ++in[v];
        ++out[i];
      }
    }
    memset(vis, 0, sizeof(vis));
    toposort.clear();
    for (int i = 1; i < nxt; ++i) {
      if (!vis[i]) dfs_topo2(i);
    }
    int dushto = 1;
    reverse(toposort.begin(), toposort.end());
    memset(vis, 0, sizeof(vis));
    for (auto u : toposort) {
      if (!vis[u]) {
        dfs2(u, dushto++);
      }
    }
    int aa = 0, bb = 0;
    for (int i = 1; i < nxt; ++i) {
      if (in[i] == 0) ++aa;
      if (out[i] == 0) ++bb;
    }
    ans = max(aa, bb);
    if (nxt - 1 == 1) ans = 0;
    printf("%d\n", ans);
    if (ans == 0) continue;
    for (int i = 1; i < dushto; ++i) {
      int p1 = i, p2 = i + 1;
      if (p2 == dushto) p2 = 1;
      used[vec2[p1][0]] = 1;
      used[vec1[p2][0]] = 1;
      edge.push_back(make_pair(vec2[p1][0], vec1[p2][0]));
    }
    for (int i = 1; i < dushto; ++i) {
      if ((int)vec1[i].size() > (int)vec2[i].size()) {
        for (int j = 1, k = 1; j < (int)vec1[i].size(); ++j, ++k) {
          if (k == (int)vec2[i].size()) k = 0;
          edge.push_back(make_pair(vec2[i][k], vec1[i][j]));
        }
      } else {
        for (int j = 1, k = 1; j < (int)vec2[i].size(); ++j, ++k) {
          if (k == (int)vec1[i].size()) k = 0;
          edge.push_back(make_pair(vec2[i][j], vec1[i][k]));
        }
      }
    }
    for (auto x : edge) {
      printf("%d %d\n", x.first, x.second);
    }
  }
}