#include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12), 0);
//int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv ./prog.exe") >> 8), 0);
vector<int> leader;
int find(int x) {
    if (leader[x] == x) return x;
    return leader[x] = find(leader[x]);
}
int main() {
    int N, Q; cin >> N >> Q;
    vector<int> C(N); for (auto &c : C) cin >> c, c--;
    leader.resize(N);
    iota(begin(leader), end(leader), 0);
    vector<map<int, int>> A(N);
    for (int i = 0; i < A.size(); i++) {
        A[i][C[i]] = 1;
    }
    for (int i = 0; i < Q; i++) {
        int type; cin >> type;
        if (type == 1) {
            int a, b; cin >> a >> b; a--, b--;
            a = find(a), b = find(b);
            if (a != b) {
                if (A[a].size() < A[b].size()) swap(a, b);
                for (auto [c, n] : A[b]) {
                    A[a][c] += n;
                }
                A[b].clear();
                leader[b] = a;
            }
        } else {
            int x, y; cin >> x >> y; x--, y--;
            cout << A[find(x)][y] << '\n';
        }
    }
    return 0;
}
// Coder: @SumitRaut
#include <bits/stdc++.h>
#define speedup ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr),cout.precision(12)
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;
#define sbc __builtin_popcount
#define pb push_back
#define em emplace
#define emb emplace_back
#define ff first
#define ss second
#define ll long long
const ll mod = 1e9+7;

template<typename T> void smax(T& a, T b) { if(a<b) a=b; }
template<typename T> void smin(T& a, T b) { if(a>b) a=b; }
template<typename T> T pw(T a,T b) { T p=1,one=1; while(b) { if(b&one) p=p*a; a=a*a; b >>=1; } return p; }
template<typename T> T pwm(T a,T b,T md=mod) { T p=1,one=1; while(b) { if(b&one) p=p*a%md; a=a*a%md; b >>=1; } return p; }

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for(auto& it:v)
    is>>it;
  return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &it : v)
    os << it << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
class Timer { chrono::high_resolution_clock::time_point start_t, end_t; public: Timer() { start_t=chrono::high_resolution_clock::now(); } ~Timer() { end_t = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::milliseconds>(end_t-start_t); cerr<<"\nRunTime: "<<duration.count()<<"ms"<<'\n'; } };
void debug() { cerr << '\n'; }
template <typename T, typename... Args>
void debug(T print, Args... args) { cerr << ' ' << print; debug(args...); }
#define deb(...) cerr << "[" << #__VA_ARGS__ << "]" << " --> ", debug(__VA_ARGS__)
#else
#define deb(...) void(0)
#endif

class dsu {
public:
    vector<int> par;
    vector<map<int,int>>Mp;

    dsu() {}

    dsu(int n_,vector<int>& ar) : par(n_, -1) {
      Mp.resize(n_);
      for(int i=0;i<n_;++i) {
        Mp[i][ar[i]]=1;
      }
    }

    int find(int cur) {
        return par[cur] < 0 ? cur : (par[cur] = find(par[cur]));
    }

    bool Union(int a, int b) {
        a = find(a), b = find(b);
        if (a != b) {
            if (par[a] > par[b])
                swap(a, b);
            par[a] += par[b];
            for(auto& it:Mp[b]) {
              Mp[a][it.ff]+=it.ss;
            }
            Mp[b].clear();
            par[b] = a;
            return 1;
        }
        return 0;
    }

    bool isConnected(int a, int b) {
        return find(a) == find(b);
    }

    int size(int a) {
        return -par[find(a)];
    }

    int cnt(int a,int k) {
      return Mp[find(a)][k];
    }

};


void solve() {
  int n,q;
  cin>>n>>q;
  vector<int> que(n);
  cin>>que;
  dsu Ds(n,que);
  while(q--) {
    int op,x,y;
    cin>>op>>x>>y;
    if(op==1) {
      Ds.Union(x-1,y-1);
    } else {
      cout<<Ds.cnt(x-1,y)<<'\n';
    }
  }
}

int main() {
  #ifndef ONLINE_JUDGE
  Timer timer;
  #endif
  speedup;
  //int t; cin>>t; while(t--)
    solve();
}
