#include <bits/stdc++.h>
using namespace std;
struct wa {
  int v;
  int child[2];
} wat;
struct wb {
  vector<wa> v;
  int child[2];
} wbt;
int n, q, ta, tb, tc, td, add, mo, ya, yb;
vector<wb> x[3];
void f2(vector<wa>& v, int a, int b, int c, int d, int e) {
  if (b == d && c == e) {
    v[a].v += add;
    return;
  }
  int bc = (b + c) / 2;
  if (d < bc) {
    if (v[a].child[0] == 0) {
      v[a].child[0] = v.size();
      v.push_back(wat);
    }
    f2(v, v[a].child[0], b, bc, d, min(e, bc));
  }
  if (e > bc) {
    if (v[a].child[1] == 0) {
      v[a].child[1] = v.size();
      v.push_back(wat);
    }
    f2(v, v[a].child[1], bc, c, max(d, bc), e);
  }
}
void f1(int a, int b, int c, int d, int e) {
  if (b == d && c == e) {
    f2(x[mo][a].v, 0, 0, 1 << 14, ya, yb);
    return;
  }
  int bc = (b + c) / 2;
  if (d < bc) {
    if (x[mo][a].child[0] == 0) {
      x[mo][a].child[0] = x[mo].size();
      x[mo].push_back(wbt);
    }
    f1(x[mo][a].child[0], b, bc, d, min(e, bc));
  }
  if (e > bc) {
    if (x[mo][a].child[1] == 0) {
      x[mo][a].child[1] = x[mo].size();
      x[mo].push_back(wbt);
    }
    f1(x[mo][a].child[1], bc, c, max(d, bc), e);
  }
}
void f4(vector<wa>& v, int a, int b, int c) {
  tc += v[a].v;
  int bc = (b + c) / 2;
  if (ya < bc) {
    if (v[a].child[0]) f4(v, v[a].child[0], b, bc);
  } else {
    if (v[a].child[1]) f4(v, v[a].child[1], bc, c);
  }
}
void f3(int a, int b, int c) {
  f4(x[mo][a].v, 0, 0, 1 << 14);
  int bc = (b + c) / 2;
  if (ta < bc) {
    if (x[mo][a].child[0]) f3(x[mo][a].child[0], b, bc);
  } else {
    if (x[mo][a].child[1]) f3(x[mo][a].child[1], bc, c);
  }
}
int main() {
  wbt.v.push_back(wat);
  for (int a = 0; a < 3; a++) x[a].push_back(wbt);
  scanf("%d", &n);
  yb = n * 2 + 2;
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &ta);
    if (ta == 1) {
      scanf("%d%d%d%d", &ta, &tb, &tc, &td);
      add = ta & 1 ? 1 : -1;
      mo = 0;
      ya = ta & 1 ? tc : tc + 1;
      if (ta < 3)
        f1(0, 0, 1 << 14, tb, tb + td + 1);
      else
        f1(0, 0, 1 << 14, tb - td, tb + 1);
      add *= -1;
      mo = abs(5 - ta * 2) == 1 ? 1 : 2;
      if (ta == 1)
        ya = tc + td + 1 + tb;
      else if (ta == 2)
        ya = tc - td - tb + n;
      else if (ta == 3)
        ya = tc + td + 1 - tb + n;
      else
        ya = tc - td + tb;
      if (ta < 3)
        f1(0, 0, 1 << 14, tb, tb + td + 1);
      else
        f1(0, 0, 1 << 14, tb - td, tb + 1);
    } else {
      scanf("%d%d", &ta, &tb);
      tc = 0;
      mo = 0;
      ya = tb;
      f3(0, 0, 1 << 14);
      mo = 1;
      ya = tb - ta + n;
      f3(0, 0, 1 << 14);
      mo = 2;
      ya = tb + ta;
      f3(0, 0, 1 << 14);
      printf("%d\n", tc);
    }
  }
  return 0;
}
