#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int v;
    struct Node* p;
};
struct Graf
{
    int v;
    int* vis;
    struct Node** p;
};
struct Node* CreatNode(int v)
{
    struct Node* n=(struct Node*)malloc(sizeof(struct Node*));
    n->v=v;
    n->p=NULL;
    return n;
}
struct Graf* CreateGraf(int v)
{
    struct Graf* g=(struct Graf*)malloc(v*sizeof(struct Graf*));
    g->v=v;
    for(int i=0;i<v;i++)
    {
        g->p[i]=NULL;
        g->vis[i]=0;
    }
    return g;
}
void AddEge(struct Graf* g,int s,int d)
{
    struct Node* n=CreatNode(d);
    n->p=g->p[s];
    g->p[s]=n->p;
    struct Node* n1=CreatNode(s);
    n1->p=g->p[d];
    g->p[d]=n1->p;
}
void DFS(struct Graf* g,int v)
{
    struct Node* t=g->p[v];
    g->vis[v]=1;
    while(t->p!=NULL)
    {
        int c=t->v;
        if(g->vis[c]==0)
            DFS(g,t->v);
        t=t->p;
    }
}
int main()
{
    int n,q,i,br=0,t,u,v;
    char c;
    scanf("%d",&n);
    scanf("%d",&q);
    struct Graf* g=CreateGraf(n);
    for(i=0;i<q;i++)
    {
        scanf("%d",&t);
        scanf("%d",&u);
        scanf("%d",&v);
        if(t==1)
        {
            scanf("%c",&c);
            AddEge(g,u,v);
        }
        else
        {
            DFS(g,u);
            if(g->vis[v]==0)
                printf("0\n");
            else
                printf("1\n");
            for(int j=0;j<n;j++)
                g->vis[j]=0;
        }
    }
    return 0;
}