#include <stdio.h>

int sets[10000];

void same(int smaller, int greater) {
  if (smaller == greater)
    printf("1\n");
  else if (sets[greater] == greater)
    printf("0\n");
  else if (sets[smaller] != smaller)
    same(sets[smaller], greater);
  else
    same(smaller, sets[greater]);
}

void unite(int smaller, int greater) {
  if (sets[greater] == greater)
    sets[greater] = sets[smaller];
  else {
    int holder = sets[greater];
    sets[greater] = sets[smaller];
    unite(smaller, holder);
  }
}

int main(void) {
  int n, q, i, j;
  scanf(" %d %d", &n, &q);
  for (i = 0; i < 10000; i++)
    sets[i] = i;
  for (i = 0; i < q; i++) {
    int command, in0, in1;
    scanf(" %d %d %d", &command, &in0, &in1);
    /* printf("%d %d %d\n", command, in0, in1); */
    /* if (!command) */
    /*   for (j = 0; j < n; j++) */
    /* 	printf("%d%c", sets[j], j==n-1?'\n':' '); */
    if (command)
      sets[in0] < sets[in1] ? same(in0, in1) : same(in1, in0);
    else {
      sets[in0] < sets[in1] ? unite(in0, in1) : unite(in1, in0);
      /* for (j = 0; j < n; j++) */
      /* 	printf("%d%c", sets[j], j==n-1?'\n':' '); */
    }
  }
  return 0;
} 	

