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 long long M = 1e9 + 7;
const long long mod = 998244353;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long binpow(long long a, long long b, long long m) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
int n, m;
vector<vector<int>> g, gt;
vector<int> used;
vector<int> order, comp;
void dfs1(int v) {
  used[v] = true;
  for (int u : g[v]) {
    if (!used[u]) dfs1(u);
  }
  order.push_back(v);
}
void dfs2(int v, int cl) {
  comp[v] = cl;
  for (int u : gt[v]) {
    if (comp[u] == -1) dfs2(u, cl);
  }
}
vector<vector<int>> dag;
vector<vector<int>> components;
vector<int> vis;
vector<int> A, B;
int w;
bool sinknotfound;
vector<int> in, out;
void search(int x) {
  vis[x] = 1;
  if (out[x] == 0) {
    w = x;
    sinknotfound = false;
    return;
  }
  for (int y : dag[x]) {
    if (sinknotfound && !vis[y]) search(y);
  }
}
void kosaraju() {
  used.assign(n, 0);
  for (int i = 0; i < n; ++i) {
    if (!used[i]) dfs1(i);
  }
  comp.assign(n, -1);
  int id = 0;
  for (int i = 0; i < n; ++i) {
    int v = order[n - i - 1];
    if (comp[v] == -1) dfs2(v, id++);
  }
  if (id == 1) {
    cout << 0 << "\n";
    return;
  }
  dag.resize(id, vector<int>());
  components = dag;
  for (int i = 0; i < n; i++) {
    components[comp[i]].push_back(i);
  }
  fill(used.begin(), used.end(), 0);
  in.resize(id);
  out = in;
  for (int i = 0; i < id; i++) {
    for (int u : components[i]) {
      for (int v : g[u]) {
        if (comp[v] != i && !used[comp[v]]) {
          used[comp[v]] = 1;
          dag[i].push_back(comp[v]);
          in[comp[v]]++;
          out[i]++;
        }
      }
    }
    for (int u : components[i]) {
      for (int v : g[u])
        if (used[comp[v]]) used[comp[v]] = 0;
    }
  }
  vis.assign(id, 0);
  vector<int> sources, sinks, uss(id);
  int t1 = 0, t2 = 0;
  for (int i = 0; i < id; i++) {
    if (in[i] == 0) {
      w = -1;
      sinknotfound = true;
      search(i);
      if (!sinknotfound) {
        A.push_back(components[i][0] + 1);
        B.push_back(components[w][0] + 1);
        uss[i] = 1;
        uss[w] = 1;
      }
      t1++;
    }
    if (out[i] == 0) {
      t2++;
    }
  }
  cout << max(t1, t2) << "\n";
  int N = A.size();
  for (int j = 0; j < N - 1; j++) {
    cout << B[j] << " " << A[j + 1] << "\n";
  }
  cout << B[N - 1] << " " << A[0] << "\n";
  for (int i = 0; i < id; i++) {
    if (!uss[i] && out[i] == 0) {
      sinks.push_back(components[i][0] + 1);
    }
    if (!uss[i] && in[i] == 0) {
      sources.push_back(components[i][0] + 1);
    }
  }
  n = sources.size();
  m = sinks.size();
  if (n >= m) {
    for (int i = 0; i < m; i++) {
      cout << sinks[i] << " " << sources[i] << "\n";
    }
    for (int i = m; i < n; i++) {
      cout << B[0] << " " << sources[i] << "\n";
    }
  } else {
    for (int i = 0; i < n; i++) {
      cout << sinks[i] << " " << sources[i] << "\n";
    }
    for (int i = n; i < m; i++) {
      cout << sinks[i] << " " << A[0] << "\n";
    }
  }
}
void solve() {
  cin >> n;
  g = gt = vector<vector<int>>(n, vector<int>());
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    g[i].push_back(x);
    gt[x].push_back(i);
  }
  kosaraju();
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cout << fixed << setprecision(12);
  while (t--) {
    solve();
  }
}