#include <iostream>
#include <unordered_map>
#include <map>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <limits>
#include <algorithm>

//#include <bits/stdc++.h>

/*
bool check(std::vector<std::vector<char>>& p, int u, int v) {
    if (seen[u] || seen[v]) return false;
    seen[u] = true;
    seen[v] = true;
    for (auto it = paths[u].begin(); it != paths[u].end(); it++) {
        char c = it->first;
        std::vector<int> pp = it->second;

        for (int p = 0; p < pp.size(); p++) {
            auto finv = paths[pp[p]][c];
            if (finv.size() > 0) {

                for (int i = 0; i < finv.size(); i++) {

                }
            }
        }
    }
}*/

int main()
{
    std::ios::sync_with_stdio(false);
    /*std::cin.tie(0);
    std::cout.tie(0);*/

    int n, q;
    std::cin >> n >> q;

    std::unordered_map<int, bool> seen;
    std::unordered_map<int, std::unordered_map<char, std::vector<int>>> paths;

    for (int i = 0; i < q; i++) {
        int t;
        std::cin >> t;
        if (t == 1) {
            int u, v;
            char c;
            std::cin >> u >> v >> c;
            paths[u][c].push_back(v);
            paths[v][c].push_back(u);
        }
        else {
            int u1, v1;
            std::cin >> u1 >> v1;

            std::cout << 1 << std::endl;

        }

    }

    return 0;
}