#include<bits/stdc++.h>
#include<string>
using namespace std;

int dfs(vector<vector<int> > graf, int u, int v, vector<bool> posecen, int d){
    if(posecen[u]) return 0;
    if(u==v) { return 1;}
    posecen[u] = true;
    for(int i=0; i<graf[u].size(); i++){
        //cout<<u<<" "<<v<<" "<<graf[u][i]<<" "<<d <<endl; 
        d += dfs(graf, graf[u][i], v, posecen, d);
        
    }
    return d;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q,t, u , v;
    string c;
    cin>>n;
    vector<vector<int> > graf(n+1);
    vector<bool> posecen(n+1);
    cin>>q;
    for(int w =0; w<q; w++){
        cin>>t;
        if(t==1){
            cin>>u>>v>>c;
            graf[u].push_back(v);
            graf[v].push_back(u);
        }
        if(t==2){
            cin>>u>>v;

            
            for(int i=0; i<=n; i++)posecen[i] = false;
            cout<<dfs(graf,u, v, posecen,0)<<endl;
        }
    }

}