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;
struct keycompare {
  bool operator()(const pair<long long, long long>& v, const long long& k) {
    return (v.first < k);
  }
  bool operator()(const long long& k, const pair<long long, long long>& v) {
    return (k < v.first);
  }
};
long long mod1 = 998244353, mod2 = 1000000007, limit = 9223372036854775807;
long double pi = 3.1415926535897932;
long long modpow(long long x, long long n, long long m) {
  if (x > m) {
    x %= m;
  }
  if (n == 0) return 1 % m;
  long long u = modpow(x, n / 2, m);
  u = (u * u) % m;
  if (n % 2 == 1) u = (u * x) % m;
  return u;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
bool isprime(long long n) {
  if (n == 2) return true;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
long long power(long long x, long long n) {
  if (n < 0) {
    return 0;
  }
  long long x_n = 1;
  for (long long i = 0; i < n; i++) {
    x_n *= x;
  }
  return x_n;
}
vector<int> adj[100005], adj2[100005];
bool visited[100005];
stack<int> st;
int parent[100005];
void dfs(int node) {
  if (visited[node]) {
    return;
  }
  visited[node] = true;
  for (auto it : adj[node]) {
    dfs(it);
  }
  st.push(node);
}
void dfs2(int node, int boss) {
  if (visited[node]) {
    return;
  }
  visited[node] = true;
  parent[node] = boss;
  for (auto it : adj2[node]) {
    dfs2(it, boss);
  }
}
set<int> adj3[100005];
set<int> avail[100005];
int indegree[100005], outdegree[100005];
vector<pair<int, int>> edge;
set<int> inin, outout, single;
bool found;
int pairing[100005], marked[100005];
void dfs3(int node, int boss) {
  if (found || visited[node]) {
    return;
  }
  visited[node] = true;
  if (outdegree[node] == 0) {
    found = true;
    pairing[boss] = 2;
    edge.push_back({boss, node});
    outout.erase(node);
  }
  for (auto it : adj3[node]) {
    dfs3(it, boss);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(pairing, 0, sizeof(pairing));
  memset(visited, 0, sizeof(visited));
  memset(indegree, 0, sizeof(indegree));
  memset(outdegree, 0, sizeof(outdegree));
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    adj[i].push_back(a);
    adj2[a].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    dfs(i);
  }
  memset(visited, 0, sizeof(visited));
  while (!st.empty()) {
    dfs2(st.top(), st.top());
    st.pop();
  }
  memset(visited, 0, sizeof(visited));
  for (int i = 1; i <= n; i++) {
    visited[parent[i]] = true;
    for (auto it : adj[i]) {
      if (parent[i] != parent[it]) {
        adj3[parent[i]].insert(parent[it]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (auto it : adj3[i]) {
      indegree[it]++;
      outdegree[i]++;
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      continue;
    }
    cnt++;
    if (indegree[i] == 0 && outdegree[i] != 0) {
      inin.insert(i);
    }
    if (outdegree[i] == 0 && indegree[i] != 0) {
      outout.insert(i);
    }
    if (indegree[i] == 0 && outdegree[i] == 0) {
      single.insert(i);
    }
  }
  if (cnt == 1) {
    cout << 0 << endl;
    return 0;
  }
  cout << max(inin.size(), outout.size()) + single.size() << "\n";
  memset(visited, 0, sizeof(visited));
  int source, sink;
  if (inin.size() > 0) {
    source = *inin.begin();
  }
  if (outout.size() > 0) {
    sink = *outout.begin();
  }
  while (inin.size() > 0 && outout.size() > 0) {
    found = false;
    pairing[*inin.begin()] = 1;
    dfs3(*inin.begin(), *inin.begin());
    inin.erase(inin.begin());
  }
  for (int i = 1; i <= n; i++) {
    if (pairing[i] == 1) {
      inin.insert(i);
    }
  }
  while (inin.size() > 0 && outout.size() > 0) {
    cout << *outout.begin() << " " << *inin.begin() << "\n";
    inin.erase(inin.begin());
    outout.erase(outout.begin());
  }
  for (auto it : single) {
    edge.push_back({it, it});
  }
  for (int i = 0; i < edge.size() - 1; i++) {
    cout << edge[i].second << " " << edge[i + 1].first << "\n";
  }
  cout << edge[edge.size() - 1].second << " " << edge[0].first << "\n";
  while (inin.size() > 0) {
    cout << sink << " " << *inin.begin() << "\n";
    inin.erase(inin.begin());
  }
  while (outout.size() > 0) {
    cout << *outout.begin() << " " << source << "\n";
    outout.erase(outout.begin());
  }
}