3
2 1 1
 6
6 2 6 2 4 2
 2
1 2
 8
2 1 2 2 6 5 6 6
 18
2 3 4 5 2 7 8 9 10 7 11 12 14 15 13 17 18 18
 7
1 2 3 4 5 6 7
 7
4 3 2 6 3 5 2
 7
7 5 3 1 2 1 5
 7
1 6 4 4 5 6 7

1
2 2 1  0
6 2 6 2 4 2  1 
1 1 
 2 
2 2 2 2 6 2 6 6 
 5 
2 3 4 5 11 7 8 9 10 11 11 11 14 15 11 17 18 11 
 6 
1 1 1 1 1 1 1 
 1
4 3 3 6 3 5 2  1
7 5 3 1 3 1 5  4 
1 6 4 1 1 1 1 

#include <bits/stdc++.h>
using LL = long long;
const int N = 200000 + 5;
int A[N], n;
int vis[N], tim;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", A + i);
  }
  std::vector<int> bad;
  for (int i = 1; i <= n; ++i) {
    if (vis[i] == 0) {
      tim++;
      for (int u = i;; u = A[u]) {
        vis[u] = tim;
        if (vis[A[u]] == tim) {
          bad.emplace_back(u);
          break;
        } else if (vis[A[u]]) {
          break;
        }
      }
    }
  }
  for (int i = 0; i < (int)bad.size(); ++i) {
    if (A[bad[i]] = bad[i]) {
      std::rotate(bad.begin(), bad.begin() + i, bad.end());
      break;
    }
  }
  int cost = (int)bad.size() - (A[bad[0]] == bad[0]);
  for (int x : bad) A[x] = bad[0];
  printf("%d\n", cost);
  for (int i = 1; i <= n; ++i) {
    printf("%d%c", A[i], " \n"[i == n]);
  }
}
