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;
long long int MOD = 1000000007;
long double EPS = 1e-9;
template <class second, class T>
ostream& operator<<(ostream& os, const pair<second, T>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const unordered_set<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class second, class T>
ostream& operator<<(ostream& os, const unordered_map<second, T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class second, class T>
ostream& operator<<(ostream& os, const map<second, T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... second>
void dbs(string str, T t, second... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class T>
void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a) cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
void pre() {}
int n, m, a, b;
vector<vector<long long int>> g[2];
int scc = 0, V[100100] = {};
stack<int> s;
void dfs(int u, int m) {
  V[u] = m == 0 ? 1 : scc;
  for (auto p : g[m][u]) {
    if (!V[p]) dfs(p, m);
  }
  if (!m) s.push(u);
}
void calc_scc() {
  for (int x = 1; x <= n; x++)
    if (!V[x]) dfs(x, 0);
  memset(V, 0, sizeof V);
  while (!s.empty()) {
    int u = s.top();
    s.pop();
    if (!V[u]) scc++, dfs(u, 1);
  }
}
vector<set<int>> sccg;
set<int> leafs;
void dfs2(int node) {
  if (sccg[node].size() == 0) {
    leafs.insert(node);
    return;
  }
  for (auto x : sccg[node]) {
    dfs2(x);
  }
}
void solve() {
  cin >> n;
  g[0].resize(n + 1);
  g[1].resize(n + 1);
  for (long long int i = 0; i < n; ++i) {
    cin >> a;
    g[0][i + 1].emplace_back(a);
    g[1][a].emplace_back(i + 1);
  }
  calc_scc();
  if (scc == 1) {
    cout << 0 << '\n';
  } else {
    sccg.resize(scc + 1);
    int indeg[scc + 1];
    int outdeg[scc + 1];
    memset(indeg, 0, sizeof(indeg));
    for (int i = 1; i <= n; i++) {
      for (auto v : g[0][i]) {
        if (V[i] != V[v]) {
          sccg[V[i]].insert(V[v]);
          indeg[V[v]]++;
        }
      }
    }
    int rep[scc + 1];
    memset(rep, 0, sizeof(rep));
    for (int i = 1; i <= n; i++) {
      if (rep[V[i]] == 0) {
        rep[V[i]] = i;
      }
    }
    vector<long long int> sources;
    vector<set<int>> sleafs;
    int cnt = 0;
    for (int i = 1; i <= scc; i++) {
      if (indeg[i] == 0) {
        leafs.clear();
        dfs2(i);
        sources.emplace_back(i);
        sleafs.emplace_back(leafs);
        cnt++;
      }
    }
    vector<pair<long long int, long long int>> ans;
    for (int i = 0; i < cnt; i++) {
      for (auto j : sleafs[i]) {
        ans.emplace_back(make_pair(j, sources[(i + 1) % cnt]));
      }
    }
    cout << ans.size() << '\n';
    for (auto x : ans) {
      cout << rep[x.first] << " " << rep[x.second] << '\n';
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  pre();
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}