//#define _GLIBCXX_DEBUG
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int INF = 1000000000;


//参照: http://satanic0258.hatenablog.com/entry/2016/04/29/004730
ll Pow(ll N, ll P){
    ll re = 1;
    for(ll i=0; i<P; ++i){
        re *= N;
    }
    return re;
}
int ans = INF;

void dfs(vector<int>& A, vector<ll> digits){
    int ret =0;
    if (A.size() == digits.size()){
        for (int i=0; i<digits.size(); i++){
            if (A[i] == 1){
                digits[i] = 0;
                ret++;
            }
        }
        int digsum =0;
        for (int i=0; i<digits.size(); i++){
            digsum += digits[i];
            digsum %= 3;
        }

        if (digsum%3 == 0 and A != vector<int>(digits.size(),1))chmin(ans,ret);
        return;
    }

    for (int v=0; v<2; v++){
        A.push_back(v);
        dfs(A, digits);
        A.pop_back();
    }
}


int main() {
    IOS;
    ll N;
    cin >> N;

    ll digit_len = 1;
    ll nd = N;
    while (nd > 10){
        nd /= 10;
        digit_len++;
    }

    vector<ll>digits(digit_len);

    for (int i=0; i<digit_len; i++){
       if (i==0)digits[i] = (N % Pow(10,i+1));
       else digits[i] = ((N % Pow(10,i+1)) - digits[i-1]) / Pow(10,i);
    }

    ll digit_sum = 0;
    
    for (int i=0; i<digit_len; i++){
        digits[i] %= 3;
        digit_sum += digits[i];
        digit_sum %= 3;
    }
    if (digit_sum == 0){
        cout << 0 << endl;
        return 0;
    }
    vector<int>Hoge;
    dfs(Hoge, digits);

    if (ans != INF) cout << ans << endl;
    else cout << -1 << endl;
}
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const v(T)&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

// Unionfind
struct uf{
  vi d;
  uf() {};
  uf(int mx) : d(mx,-1) {};
  int root(int x){
    if(d[x]<0) return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y){
    x = root(x); y = root(y);
    if(x == y) return false;
    if(d[x] > d[y]) swap(x,y);
    d[x] += d[y]; d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y);}
  int size(int x){ return -d[root(x)];}
  int operator[] (int x) {return root(x);}
  int operator() (int x) {return size(x);}
};

int main() {
  int n, q; cin >> n >> q;
  vi c(n);
  rep(i,n) cin >> c[i], --c[i];

  vector<set<int> > s(n); // 1次元目が根の２次元目にその根がどのような種類のクラスを保持しているか
  vector<map<int,int> > m(n); // 1次元目が根の２次元目で各クラスに何人いるかを保持
  rep(i,n){
    s[i].insert(c[i]);
    m[i][c[i]] = 1;
  }

  uf dis(n);

  vi ans;
  rep(_,q){
    int qi; cin >> qi;
    if(qi == 1){
      int a,b; cin >> a >> b; --a,--b;
      if(dis.same(a,b)) continue;
      if(dis.size(a) < dis.size(b)) swap(a,b);
      int ra = dis.root(a), rb = dis.root(b);
      for(int clsb : s[rb]){
        if(s[ra].count(clsb)) m[ra][clsb] += m[rb][clsb];
        else s[ra].insert(clsb), m[ra][clsb] = m[rb][clsb];
      }
      // printf("ra: %d m[ra][1]: %d\n", ra, m[ra][1]);
      dis.unite(a,b);

    }else{
      int x,y; cin >> x >> y; --x,--y;
      ans.push_back(m[dis.root(x)][y]);
    }
  }
  for(int res: ans) cout << res << endl;
  return 0;
}
