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;
void setIO(const string &name) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin.exceptions(istream::failbit);
}
const int inf = 0x3f3f3f3f, mod = 1e9 + 7, maxn = 1e5 + 5;
int n;
vector<int> adj[maxn], radj[maxn];
int comp[maxn];
bool vis[maxn];
vector<int> todo, components;
void dfs1(int c) {
  vis[c] = true;
  for (auto &i : adj[c]) {
    if (!vis[i]) {
      dfs1(i);
    }
  }
  todo.push_back(c);
}
void dfs2(int c, int v) {
  comp[c] = v;
  for (auto &i : radj[c]) {
    if (comp[i] == -1) {
      dfs2(i, v);
    }
  }
}
void gen() {
  memset(comp, -1, sizeof(comp));
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      dfs1(i);
    }
  }
  reverse(todo.begin(), todo.end());
  for (auto &i : todo) {
    if (comp[i] == -1) {
      dfs2(i, i);
      components.push_back(i);
    }
  }
}
vector<int> graph[maxn];
queue<int> st[maxn], en[maxn];
int in_deg[maxn], weak_comp[maxn], out_deg[maxn];
void dfs3(int c, int v) {
  vis[c] = true;
  weak_comp[c] = v;
  for (auto &i : adj[c]) {
    if (!vis[i]) {
      dfs3(i, v);
    }
  }
  for (auto &i : radj[c]) {
    if (!vis[i]) {
      dfs3(i, v);
    }
  }
}
queue<int> fin_st, fin_en;
int main() {
  setIO("4");
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    --a;
    adj[i].push_back(a);
    radj[a].push_back(i);
  }
  gen();
  memset(vis, false, sizeof(vis));
  vector<int> weak;
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      weak.push_back(i);
      dfs3(i, i);
    }
  }
  for (int i = 0; i < n; ++i) {
    for (auto &j : adj[i]) {
      if (comp[i] != comp[j]) {
        graph[comp[i]].push_back(comp[j]);
        ++in_deg[comp[j]];
        ++out_deg[comp[i]];
      }
    }
  }
  set<pair<int, int>> sol;
  for (int i = 0; i < n; ++i) {
    if (comp[i] == i) {
      if (in_deg[i] == 0) {
        st[weak_comp[i]].push(i);
      }
      if (out_deg[i] == 0) {
        en[weak_comp[i]].push(i);
      }
    }
  }
  int last_st, last_en;
  for (int i = 0; i < int(weak.size()); ++i) {
    sol.insert({en[weak[i]].front(), st[weak[(i + 1) % weak.size()]].front()});
    last_en = en[weak[i]].front(),
    last_st = st[weak[(i + 1) % weak.size()]].front();
    en[weak[i]].pop();
    st[weak[(i + 1) % weak.size()]].pop();
    while (!en[weak[i]].empty()) {
      fin_en.push(en[weak[i]].front());
      en[weak[i]].pop();
    }
    while (!st[weak[(i + 1) % weak.size()]].empty()) {
      fin_st.push(st[weak[(i + 1) % weak.size()]].front());
      st[weak[(i + 1) % weak.size()]].pop();
    }
  }
  if (fin_st.empty()) {
    fin_st.push(last_st);
  }
  if (fin_en.empty()) {
    fin_en.push(last_en);
  }
  while (fin_en.size() > 1) {
    int j = fin_en.front();
    fin_en.pop();
    sol.insert({j, fin_st.front()});
    if (fin_st.size() > 1) {
      fin_st.pop();
    }
  }
  while (!fin_st.empty()) {
    int j = fin_st.front();
    fin_st.pop();
    sol.insert({fin_en.front(), j});
  }
  vector<pair<int, int>> fin;
  for (auto &i : sol) {
    if (i.first != i.second) {
      fin.emplace_back(i.first, i.second);
    }
  }
  cout << fin.size() << "\n";
  for (auto &i : fin) {
    cout << i.first + 1 << " " << i.second + 1 << "\n";
  }
  return 0;
}