#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma comment(linker, "/stack:200000000")
using namespace std;
namespace IO {
const int BUFFER_SIZE = 1 << 15;
char input_buffer[BUFFER_SIZE];
int input_pos = 0, input_len = 0;
void _update_input_buffer() {
  input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin);
  input_pos = 0;
  if (input_len == 0) input_buffer[0] = EOF;
}
inline char next_char(bool advance = true) {
  if (input_pos >= input_len) _update_input_buffer();
  return input_buffer[advance ? input_pos++ : input_pos];
}
template <typename T>
inline void read_int(T &number) {
  bool negative = false;
  number = 0;
  while (!isdigit(next_char(false)))
    if (next_char() == '-') negative = true;
  do {
    number = 10 * number + (next_char() - '0');
  } while (isdigit(next_char(false)));
  if (negative) number = -number;
}
template <typename T, typename... Args>
inline void read_int(T &number, Args &...args) {
  read_int(number);
  read_int(args...);
}
}  // namespace IO
const int V_MAX = 7005;
int N, Q;
bitset<V_MAX> square_free;
bitset<V_MAX> mobius[V_MAX];
vector<bitset<V_MAX>> values;
int main() {
  square_free.set();
  for (int i = 1; i < V_MAX; i++)
    for (int p = 2; p * p <= i; p++)
      if (i % (p * p) == 0) square_free[i] = 0;
  for (int i = 1; i < V_MAX; i++)
    for (int j = 1; i * j < V_MAX; j++) mobius[i][i * j] = square_free[j];
  IO::read_int(N, Q);
  values.resize(N);
  for (int q = 0; q < Q; q++) {
    int type, x, y, z;
    IO::read_int(type, x, y);
    x--;
    if (type == 2 || type == 3) {
      IO::read_int(z);
      y--;
      z--;
    }
    if (type == 1) {
      values[x].reset();
      for (int i = 1; i * i <= y; i++)
        if (y % i == 0) values[x][i] = values[x][y / i] = 1;
    } else if (type == 2) {
      values[x] = values[y] ^ values[z];
    } else if (type == 3) {
      values[x] = values[y] & values[z];
    } else if (type == 4) {
      putchar((values[x] & mobius[y]).count() % 2 + '0');
    }
  }
  putchar('\n');
}
