#include <bits/stdc++.h>
#define il inline
#define ri register int
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 5050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define repd(i, x, y) for(ri i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".my", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
	read(args...);
}
int n, m, q;
struct BIT1 {
	int a[MAXN][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} O;
struct BIT2 {
	int a[MAXN*2][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} T[2];
int main() {
	read(n, q), m = n * 2;
	int op, dir, x, y, len;
	while(q--) {
		read(op);
		if(op == 1) {
			read(dir, x, y, len);
			if(dir == 1) O.add(x, n, y, y+len, 1), T[0].add(x+y+len+1, n, y, y+len, -1);
			if(dir == 2) O.add(x, n, y-len, y, 1), T[1].add(1, y-len+n-x-1, y-len, y, -1);
			if(dir == 3) O.add(1, x, y, y+len, 1), T[1].add(y+len+n-x+1, n, y, y+len, -1);
			if(dir == 4) O.add(1, x, y-len, y, 1), T[0].add(1, x+y-len-1, y-len, y, -1);
		}
		else read(x, y), printf("%d\n", T[0].query(x, y) + T[1].query(x+y, y) + T[2].query(y+n-x, y));
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define ri register int
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 5050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define repd(i, x, y) for(ri i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".my", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
	read(args...);
}
int n, m, q;
struct BIT1 {
	int a[MAXN][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} O;
struct BIT2 {
	int a[MAXN*2][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		if(x1 > x2 || y1 > y2) return ;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} T[2];
int main() {
	read(n, q), m = n * 2;
	int op, dir, x, y, len;
	while(q--) {
		read(op);
		if(op == 1) {
			read(dir, x, y, len);
			if(dir == 1) O.add(x, n, y, y+len, 1), T[0].add(x+y+len+1, n, y, y+len, -1);
			if(dir == 2) O.add(x, n, y-len, y, 1), T[1].add(1, y-len+n-x-1, y-len, y, -1);
			if(dir == 3) O.add(1, x, y, y+len, 1), T[1].add(y+len+n-x+1, n, y, y+len, -1);
			if(dir == 4) O.add(1, x, y-len, y, 1), T[0].add(1, x+y-len-1, y-len, y, -1);
		}
		else read(x, y), printf("%d\n", O.query(x, y) + T[0].query(x+y, y) + T[1].query(y+n-x, y));
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define ri register int
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 5050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define repd(i, x, y) for(ri i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".my", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
	read(args...);
}
int n, m, q;
struct BIT1 {
	int a[MAXN][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} O;
struct BIT2 {
	int a[MAXN*2][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} T[2];
int main() {
	read(n, q), m = n * 2;
	int op, dir, x, y, len;
	while(q--) {
		read(op);
		if(op == 1) {
			read(dir, x, y, len);
			if(dir == 1) O.add(x, n, y, y+len, 1), T[0].add(x+y+len+1, n, y, y+len, -1);
			if(dir == 2) O.add(x, n, y-len, y, 1), T[1].add(1, y-len+n-x-1, y-len, y, -1);
			if(dir == 3) O.add(1, x, y, y+len, 1), T[1].add(y+len+n-x+1, n, y, y+len, -1);
			if(dir == 4) O.add(1, x, y-len, y, 1), T[0].add(1, x+y-len-1, y-len, y, -1);
		}
		else read(x, y), printf("%d\n", O.query(x, y) + T[0].query(x+y, y) + T[1].query(y+n-x, y));
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define ri register int
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 5050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define repd(i, x, y) for(ri i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".my", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
	read(args...);
}
int n, m, q;
struct BIT1 {
	int a[MAXN][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} O;
struct BIT2 {
	int a[MAXN*2][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		if(x1 > x2 || y1 > y2) return ;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} T[2];
int main() {
	read(n, q), m = n * 2;
	int op, dir, x, y, len;
	while(q--) {
		read(op);
		if(op == 1) {
			read(dir, x, y, len);
			if(dir == 1) O.add(x, n, y, y+len, 1), T[0].add(x+y+len+1, m, y, y+len, -1);
			if(dir == 2) O.add(x, n, y-len, y, 1), T[1].add(1, y-len+n-x-1, y-len, y, -1);
			if(dir == 3) O.add(1, x, y, y+len, 1), T[1].add(y+len+n-x+1, m, y, y+len, -1);
			if(dir == 4) O.add(1, x, y-len, y, 1), T[0].add(1, x+y-len-1, y-len, y, -1);
		}
		else read(x, y), printf("%d\n", O.query(x, y) + T[0].query(x+y, y) + T[1].query(y+n-x, y));
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define ri register int
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 5050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define repd(i, x, y) for(ri i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".my", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
	read(args...);
}
int n, m, q;
struct BIT1 {
	int a[MAXN][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= n; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} O;
struct BIT2 {
	int a[MAXN*2][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		if(x1 > x2 || y1 > y2) return ;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} T[2];
int main() {
	read(n, q), m = n * 2;
	int op, dir, x, y, len;
	while(q--) {
		read(op);
		if(op == 1) {
			read(dir, x, y, len);
			if(dir == 1) O.add(x, n, y, y+len, 1), T[0].add(x+y+len+1, m, y, y+len, -1);
			if(dir == 2) O.add(x, n, y-len, y, 1), T[1].add(1, y-len+n-x-1, y-len, y, -1);
			if(dir == 3) O.add(1, x, y, y+len, 1), T[1].add(y+len+n-x+1, m, y, y+len, -1);
			if(dir == 4) O.add(1, x, y-len, y, 1), T[0].add(1, x+y-len-1, y-len, y, -1);
		}
		else read(x, y), printf("%d\n", O.query(x, y) + T[0].query(x+y, y) + T[1].query(y+n-x, y));
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define ri register int
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 5050
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define repd(i, x, y) for(ri i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".my", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
	read(args...);
}
int n, m, q;
struct BIT {
	int a[MAXN*2][MAXN];
	void add(int x, int y, int w) {
		for(ri i = x; i <= m; i += i & -i)
			for(ri j = y; j <= n; j += j & -j)
				a[i][j] += w;
	}
	int query(int x, int y) {
		int ret = 0;
		for(ri i = x; i; i -= i & -i)
			for(ri j = y; j; j -= j & -j)
				ret += a[i][j];
		return ret;
	}
	void add(int x1, int x2, int y1, int y2, int w) {
//		cout<<"add: "<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<' '<<w<<endl;
		add(x1, y1, w), add(x2+1, y1, -w), add(x1, y2+1, -w), add(x2+1, y2+1, w);
	}
} T[3];
int main() {
	read(n, q), m = n * 2;
	int op, dir, x, y, len;
	while(q--) {
		read(op);
		if(op == 1) {
			read(dir, x, y, len);
			if(dir == 1) T[0].add(x, n, y, y+len, 1), T[1].add(x+y+len+1, n, y, y+len, -1);
			if(dir == 2) T[0].add(x, n, y-len, y, 1), T[2].add(1, y-len+n-x-1, y-len, y, -1);
			if(dir == 3) T[0].add(1, x, y, y+len, 1), T[2].add(y+len+n-x+1, n, y, y+len, -1);
			if(dir == 4) T[0].add(1, x, y-len, y, 1), T[1].add(1, x+y-len-1, y-len, y, -1);
		}
		else read(x, y), printf("%d\n", T[0].query(x, y) + T[1].query(x+y, y) + T[2].query(y+n-x, y));
	}
	return 0;
}


#include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}
template<class Type> Type read() {
	Type a;
	bool b;
	unsigned char c;
	while(c=getchar()-48, (c>9)&(c!=253));
	for(a=(b=c==253)?0:c; (c=getchar()-48)<=9; a=a*10+c);
	return b?-a:a;
}
int (*rd)()=read<int>;
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;

struct Point {
	int x, y, val, *ans;
};
const int Q=1e5;
int n, op[Q], dir[Q], x[Q], y[Q], len[Q], ans[Q], bit[5001];
Point p[3*Q], save[3*Q];
int bit_par_sum(int x) {
	int res=0;
	for(; x; x&=x-1) res+=bit[x];
	return res;
}
void bit_inc(int x, int v) {
	for(; x<=n; x+=x&-x) bit[x]+=v;
}
void bit_reset(int x) {
	for(; x<=n; x+=x&-x) bit[x]=0;
}
void solve(int l, int r) {
	if(r-l==1) return;
	int mid=(l+r)/2;
	solve(l, mid);
	solve(mid, r);
	int i=l, j=mid, k=l;
	while(i<mid&&j<r)
		if(p[i].x<=p[j].x) {
			if(p[i].val) bit_inc(p[i].y, p[i].val);
			save[k++]=p[i++];
		} else {
			if(p[j].ans) *p[j].ans+=bit_par_sum(p[j].y);
			save[k++]=p[j++];
		}
	while(i<mid) save[k++]=p[i++];
	while(j<r) {
		if(p[j].ans) *p[j].ans+=bit_par_sum(p[j].y);
		save[k++]=p[j++];
	}
	for(i=l; i<mid; ++i) if(p[i].val) bit_reset(p[i].y);
	for(i=l; i<r; ++i) p[i]=save[i];
}
int main() {
	int q, p_cnt;
	n=rd(), q=rd();
	for(int i=0; i<q; ++i) {
		op[i]=rd();
		if(op[i]==1) {
			dir[i]=rd(), x[i]=rd(), y[i]=rd(), len[i]=rd();
		} else {
			x[i]=rd(), y[i]=rd();
		}
	}
	//x+y, x
	p_cnt=0;
	for(int i=0; i<q; ++i) {
		if(op[i]==2) {
			p[p_cnt++]={x[i]+y[i], x[i], 0, ans+i};
		} else if(dir[i]==1) {
			p[p_cnt++]={x[i]+y[i], x[i], 1, NULL};
			p[p_cnt++]={x[i]+y[i]+len[i]+1, x[i], -1, NULL};
		} else if(dir[i]==4) {
			p[p_cnt++]={x[i]+y[i]-len[i], x[i]+1, -1, NULL};
			p[p_cnt++]={x[i]+y[i]+1, x[i]+1, 1, NULL};
		}
	}
	solve(0, p_cnt);
	//x+y, y
	p_cnt=0;
	for(int i=0; i<q; ++i) {
		if(op[i]==2) {
			p[p_cnt++]={x[i]+y[i], n+1-y[i], 0, ans+i};
		} else if(dir[i]==1) {
			p[p_cnt++]={x[i]+y[i], n+2-y[i], -1, NULL};
			p[p_cnt++]={x[i]+y[i]+len[i]+1, n+2-y[i], 1, NULL};
		} else if(dir[i]==4) {
			p[p_cnt++]={x[i]+y[i]-len[i], n+1-y[i], 1, NULL};
			p[p_cnt++]={x[i]+y[i]+1, n+1-y[i], -1, NULL};
		}
	}
	solve(0, p_cnt);
	//x-y, x
	p_cnt=0;
	for(int i=0; i<q; ++i) {
		if(op[i]==2) {
			p[p_cnt++]={x[i]-y[i], x[i], 0, ans+i};
		} else if(dir[i]==2) {
			p[p_cnt++]={x[i]-y[i], x[i], 1, NULL};
			p[p_cnt++]={x[i]-y[i]+len[i]+1, x[i], -1, NULL};
		} else if(dir[i]==3) {
			p[p_cnt++]={x[i]-y[i]-len[i], x[i]+1, -1, NULL};
			p[p_cnt++]={x[i]-y[i]+1, x[i]+1, 1, NULL};
		}
	}
	solve(0, p_cnt);
	//x-y, y
	p_cnt=0;
	for(int i=0; i<q; ++i) {
		if(op[i]==2) {
			p[p_cnt++]={x[i]-y[i], y[i], 0, ans+i};
		} else if(dir[i]==2) {
			p[p_cnt++]={x[i]-y[i], y[i]+1, -1, NULL};
			p[p_cnt++]={x[i]-y[i]+len[i]+1, y[i]+1, 1, NULL};
		} else if(dir[i]==3) {
			p[p_cnt++]={x[i]-y[i]-len[i], y[i], 1, NULL};
			p[p_cnt++]={x[i]-y[i]+1, y[i], -1, NULL};
		}
	}
	solve(0, p_cnt);
	for(int i=0; i<q; ++i) if(op[i]==2) printf("%d\n", ans[i]);
	return 0;
}


#include <bits/stdc++.h>
#define lowbit(x) (x&-x)

using namespace std;

int sumv[5005][10005];

void add(int x,int y,int num) {
  for(int i=x;i;i-=lowbit(i))
    for(int j=y;j;j-=lowbit(j)) sumv[i][j]+=num;
}

int sum(int x,int y,int n,int m) {
  int s=0;
  for(int i=x;i<=n;i+=lowbit(i))
    for(int j=y;j<=m;j+=lowbit(j)) s+=sumv[i][j];
  return s;
}

void add(int l1,int r1,int l2,int r2,int num) {
  add(r1,r2,num);
  add(r1,l2-1,-num);
  add(l1-1,r2,-num);
  add(l1-1,l2-1,num);
}

struct Data {
  int kind,x,y,d;
  Data() {}
  Data(int a,int b,int c,int d):kind(a),x(b),y(c),d(d) {}
};

Data a[100005];
int ans[100005];

int main() {
  int n,m;
  scanf("%d%d",&n,&m);
  for(int i=1;i<=m;i++) {
  	int kind;
  	scanf("%d",&kind);
  	if (kind==1) {
  		int aa,bb,cc,dd;
  	    scanf("%d%d%d%d",&aa,&bb,&cc,&dd);
  	    a[i]=Data(aa,bb,cc,dd);
	  }
	else {
		int x,y;
		scanf("%d%d",&x,&y);
		a[i]=Data(5,x,y,0);
	}
  }
  memset(sumv,0,sizeof(sumv));
  for(int i=1;i<=m;i++) {
  	if (a[i].kind==1) add(1,a[i].x-1,1,a[i].y-1,1);
  	if (a[i].kind==2) add(1,a[i].x-1,a[i].y+1,n,1);
  	if (a[i].kind==3) add(a[i].x+1,n,1,a[i].y-1,1);
  	if (a[i].kind==4) add(a[i].x+1,n,a[i].y+1,n,1);
  	if (a[i].kind==5) ans[i]+=sum(a[i].x,a[i].y,n,n);
  }
  memset(sumv,0,sizeof(sumv));
  for(int i=1;i<=m;i++) {
  	if (a[i].kind==1) add(a[i].x,n,1,a[i].x+a[i].y+a[i].d,1);
  	if (a[i].kind==4) add(1,a[i].x,a[i].x+a[i].y-a[i].d,n*2,1);
  	if (a[i].kind==5) ans[i]+=sum(a[i].x,a[i].x+a[i].y,n,n*2);
  }
  memset(sumv,0,sizeof(sumv));
  for(int i=1;i<=m;i++) {
  	if (a[i].kind==1) add(1,a[i].y-1,1,a[i].x+a[i].y+a[i].d,-1);
  	if (a[i].kind==4) add(a[i].y+1,n,a[i].x+a[i].y-a[i].d,n*2,-1);
  	if (a[i].kind==5) ans[i]+=sum(a[i].y,a[i].x+a[i].y,n,n*2);
  }
  memset(sumv,0,sizeof(sumv));
  for(int i=1;i<=m;i++) {
  	if (a[i].kind==2) add(a[i].x,n,1,a[i].x-a[i].y+a[i].d+n,1);
  	if (a[i].kind==3) add(1,a[i].x,a[i].x-a[i].y-a[i].d+n,2*n,1);
  	if (a[i].kind==5) ans[i]+=sum(a[i].x,a[i].x-a[i].y+n,n,n*2);
  }
  memset(sumv,0,sizeof(sumv));
  for(int i=1;i<=m;i++) {
  	if (a[i].kind==2) add(a[i].y+1,n,1,a[i].x-a[i].y+a[i].d+n,-1);
  	if (a[i].kind==3) add(1,a[i].y-1,a[i].x-a[i].y-a[i].d+n,2*n,-1);
  	if (a[i].kind==5) ans[i]+=sum(a[i].y,a[i].x-a[i].y+n,n,n*2);
  }
  for(int i=1;i<=m;i++)
    if (a[i].kind==5) printf("%d\n",ans[i]);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int bit1[5555][5555],bit2[11111][5555],bit3[11111][5555];
int n,q;
int main()
{
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int bit1[5555][5555],bit2[11111][5555],ans[111111],x,y,len,dir;
int n,q,que1[111111],que2[111111],que3[111111],que4[111111],que5[111111];
void update1(int x,int y,int p)
{
	for (int i=x;i<=n;i+=i&(-i))
	{
		for (int j=y;j<=n;j+=j&(-j))
		{
			bit1[i][j]+=p;
		}
	}
}
void update2(int x,int y,int p)
{
	for (int i=x;i<=2*n;i+=i&(-i))
	{
		for (int j=y;j<=n;j+=j&(-j))
		{
			bit2[i][j]+=p;
		}
	}
}
int query1(int x,int y)
{
	if (x<=0 || y<=0) return 0;
	int res=0;
	for (int i=x;i;i-=i&(-i))
	{
		for (int j=y;j;j-=j&(-j))
		{
			res+=bit1[i][j];
		}
	}
	return res;
}
int query2(int x,int y)
{
	int res=0;
	for (int i=x;i;i-=i&(-i))
	{
		for (int j=y;j;j-=j&(-j))
		{
			res+=bit2[i][j];
		}
	}
	return res;
}
int main()
{
	scanf("%d%d",&n,&q);
	for (int i=1;i<=q;i++)
	{
		scanf("%d",&que1[i]);
		if (que1[i]==1)
		{
			scanf("%d%d%d%d",&que2[i],&que3[i],&que4[i],&que5[i]);
		}
		else
		{
			scanf("%d%d",&que2[i],&que3[i]);
		}
	}
	for (int i=1;i<=q;i++)
	{
		if (que1[i]==1)
		{
			dir=que2[i];x=que3[i];y=que4[i];len=que5[i];
			if (dir==1)
			{
				update1(x,y,1);
				if (x+len+1<=n) update1(x+len+1,y,-1);
				if (y+len+1<=n)
				{
					update2(x+(y+len+1),x,-1);
					if (x+len+1<=n) update2((x+len+1)+y,x+len+1,1);
				}
				else
				{
					update2(x+(y+len+1),x+1,-1);
					if (x+len+1<=n) update2((x+len+1)+y,x+len+1,1);
				}
			}
			else if (dir==4)
			{
				update2(x+y-len,x-len,1);
				if (x+1<=n) update2(x+y-len,x+1,-1);
				if (y+1<=n)
				{
					update1(x-len,y+1,-1);
					if (x+1<=n) update1(x+1,y+1,1);
				}
			}
		}
		else
		{
			x=que2[i];y=que3[i];
			ans[i]=query1(x,y)+query2(x+y,x)-query2(x,x);
		}
	}
	memset(bit1,0,sizeof(bit1));
	memset(bit2,0,sizeof(bit2));
	for (int i=1;i<=q;i++)
	{
		if (que1[i]==1)
		{
			dir=que2[i];x=que3[i];y=que4[i];len=que5[i];
			if (dir==2)
			{
				update2(x-(y-len)+n,x,1);
				if (x+len+1<=n)
				{
					update2(x-(y-len)+n,x+len+1,-1);
				}
				if (y+1<=n)
				{
					update1(x,y+1,-1);
					if (x+len+1<=n) update1(x+len+1,y+1,1);
				}
			}
			else if (dir==3)
			{
				update1(x-len,y,1);
				if (x+1<=n) update1(x+1,y,-1);
				if (y+1<=n)
				{
					update2(x-(y+len+1)+n,x-len,-1);
					if (x+1<=n && y+len+2<=n) update2(x-(y+len+1)+n,x+1,1);
				}
			}
		}
		else
		{
			x=que2[i];y=que3[i];
			ans[i]+=(query1(x,y)+query2(x-1+n,x)-query2(x-(y+1)+n,x));
		}
	}
	for (int i=1;i<=q;i++)
	{
		if (que1[i]!=1) printf("%d\n",ans[i]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=5005;
const int maxm=100005;
int n,q,dir;
int qt[maxm],qd[maxm],qx[maxm],qy[maxm],qlen[maxm],ans[maxm];

namespace Segtree{
	const int maxs=maxn*500;
	int tot;
	int ls[maxs],rs[maxs],rtx[maxs],rty[maxs],sum[maxs],tots[maxs];
	void clear(int cur,int l,int r){
		tot=0;
		rtx[cur]=rty[cur]=tots[cur]=0;
		if(l==r)return;
		int mid=l+r>>1;
		clear(cur<<1,l,mid);
		clear(cur<<1|1,mid+1,r);
	}
	void sub_update(int &cur,int l,int r,int vl,int vr){
		if(vl>vr)return;
		if(!cur){
			cur=++tot;
			ls[cur]=rs[cur]=sum[cur]=0;
		}
		if(l>=vl&&r<=vr){
			sum[cur]++;
			return;
		}
		int mid=l+r>>1;
		if(mid>=vl)sub_update(ls[cur],l,mid,vl,vr);
		if(mid<vr)sub_update(rs[cur],mid+1,r,vl,vr);
	}
	void update(int cur,int l,int r,int vl,int vr,int vx,int vy){
		if(l>=vl&&r<=vr){
			tots[cur]++;
			sub_update(rtx[cur],1,n,1,vx);
			sub_update(rty[cur],1,n,1,vy);
			return;
		}
		int mid=l+r>>1;
		if(mid>=vl)update(cur<<1,l,mid,vl,vr,vx,vy);
		if(mid<vr)update(cur<<1|1,mid+1,r,vl,vr,vx,vy);
	}
	int sub_query(int cur,int l,int r,int k){
		if(!cur)return 0;
		int res=sum[cur];
		if(l==r)return res;
		int mid=l+r>>1;
		if(k<=mid)res+=sub_query(ls[cur],l,mid,k);
		else res+=sub_query(rs[cur],mid+1,r,k);
		return res;
	}
	int query(int cur,int l,int r,int k,int kx,int ky){
		int res=tots[cur]-sub_query(rtx[cur],1,n,kx)-sub_query(rty[cur],1,n,ky);
		if(l==r)return res;
		int mid=l+r>>1;
		if(k<=mid)res+=query(cur<<1,l,mid,k,kx,ky);
		else res+=query(cur<<1|1,mid+1,r,k,kx,ky);
		return res;
	}
}

void solve(int dir){
	Segtree::clear(1,1,2*n);
	REP(i,q){
		if(qt[i]==2){
			ans[i]+=Segtree::query(1,1,2*n,qx[i]+qy[i],qx[i],qy[i]);
		}
		else if(qd[i]==dir){
			Segtree::update(1,1,2*n,qx[i]+qy[i],qx[i]+qy[i]+qlen[i],qx[i]-1,qy[i]-1);
		}
	}
}

int main(){
	scanf("%d%d",&n,&q);
	REP(i,q){
		scanf("%d",&qt[i]);
		if(qt[i]==1)scanf("%d%d%d%d",&qd[i],&qx[i],&qy[i],&qlen[i]);
		else scanf("%d%d",&qx[i],&qy[i]);
	}
	solve(1);
	REP(i,q)qy[i]=n-qy[i]+1;
	solve(2);
	REP(i,q)qy[i]=n-qy[i]+1,qx[i]=n-qx[i]+1;
	solve(3);
	REP(i,q)qy[i]=n-qy[i]+1;
	solve(4);
	REP(i,q)if(qt[i]==2)printf("%d\n",ans[i]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node
{
	int x,y;
	int type;
	int c;
	int id;
	Node(const int t=0,const int i=0,const int xx=0,const int yy=0,const int cc=0)
	{
		type=t,id=i;
		x=xx,y=yy,c=cc;
	}
}a[1000005],tmp[1000005];
int n,q,tot,ans[1000005],top,c[1000005],c0[1000005],c1[1000005];
bool flag[1000005];
void add(int p,int v,int*c)
{
	while(p<=2*n)
	{
		c[p]+=v;
		p+=p^(p&(p-1));
	}
}
int sum(int p,int*c)
{
	int ans=0;
	while(p)
	{
		ans+=c[p];
		p&=p-1;
	}
	return ans;
}
void solve(int l,int r)
{
	if(l==r)return;
	int mid=(l+r)>>1;
	solve(l,mid);
	solve(mid+1,r);
	top=0;
	int now1=l,now2=mid+1;
//	printf("[%d,%d]:\n",l,r);
	while(now1<=mid||now2<=r)
	{
		while(now1<=mid&&a[now1].type==0)now1++;
		while(now2<=r&&a[now2].type!=0)now2++;
		if(now1>mid&&now2>r)break;
		if(now1>mid)tmp[++top]=a[now2++];
		else if(now2>r)tmp[++top]=a[now1++];
		else
		{
			if(a[now1].x<a[now2].x)tmp[++top]=a[now1++];
			else tmp[++top]=a[now2++];
		}
//		printf("type=%d,id=%d,(%d,%d),c=%d\n",tmp[top].type,tmp[top].id,tmp[top].x,tmp[top].y,tmp[top].c);
		if(tmp[top].type==1)
		{
			add(1,tmp[top].c,c0);
			add(tmp[top].x+tmp[top].y+1,-tmp[top].c,c0);
		}
		else if(tmp[top].type==2)
		{
			add(1,tmp[top].c,c1);
			add(tmp[top].x-tmp[top].y+n+1,-tmp[top].c,c1);
		}
		else if(tmp[top].type==3)
		{
			add(1,tmp[top].c,c);
			add(tmp[top].y+1,-tmp[top].c,c);
		}
		else if(tmp[top].type==4)
		{
			add(tmp[top].y,tmp[top].c,c);
		}
		else if(tmp[top].type==0)
			ans[tmp[top].id]+=sum(tmp[top].y,c)+sum(tmp[top].x+tmp[top].y,c0)+sum(tmp[top].x-tmp[top].y+n,c1);
	}
	for(int i=1;i<=top;i++)
	{
		if(tmp[i].type==1)
		{
			add(1,-tmp[i].c,c0);
			add(tmp[i].x+tmp[i].y+1,tmp[i].c,c0);
		}
		else if(tmp[i].type==2)
		{
			add(1,-tmp[i].c,c1);
			add(tmp[i].x-tmp[i].y+n+1,tmp[i].c,c1);
		}
		else if(tmp[i].type==3)
		{
			add(1,-tmp[i].c,c);
			add(tmp[i].y+1,tmp[i].c,c);
		}
		else if(tmp[i].type==4)
		{
			add(tmp[i].y,-tmp[i].c,c);
		}
	}
	top=0;
	now1=l,now2=mid+1;
	while(now1<=mid||now2<=r)
	{
		if(now1>mid)tmp[++top]=a[now2++];
		else if(now2>r)tmp[++top]=a[now1++];
		else
		{
			if(a[now1].x<a[now2].x)tmp[++top]=a[now1++];
			else tmp[++top]=a[now2++];
		}
	}
	for(int i=1;i<=top;i++)
		a[l+i-1]=tmp[i];
}
int main()
{
	scanf("%d%d",&n,&q);
	for(int i=1;i<=q;i++)
	{
		int type;
		scanf("%d",&type);
		if(type==1)
		{
			int dir,x,y,len;
			scanf("%d%d%d%d",&dir,&x,&y,&len);
			if(dir==1)
			{
				a[++tot]=Node(1,i,x,y+len,1);
				if(x+len+1<=n&&y-1>=1)a[++tot]=Node(1,i,x+len+1,y-1,-1);
				if(y-1>=1)a[++tot]=Node(3,i,x,y-1,-1);
				if(x+len+1<=n&&y-1>=1)a[++tot]=Node(3,i,x+len+1,y-1,1);
			}
			if(dir==2)
			{
				a[++tot]=Node(2,i,x,y-len,1);
				if(x+len+1<=n&&y+1<=n)a[++tot]=Node(2,i,x+len+1,y+1,-1);
				if(y+1<=n)a[++tot]=Node(4,i,x,y+1,-1);
				if(x+len+1<=n&&y+1<=n)a[++tot]=Node(4,i,x+len+1,y+1,1);
			}
			if(dir==3)
			{
				a[++tot]=Node(4,i,x-len,y,1);
				if(x+1<=n)a[++tot]=Node(4,i,x+1,y,-1);
				if(y+1<=n)a[++tot]=Node(2,i,x-len,y+1,-1);
				if(x+1<=n&&y+len+1<=n)a[++tot]=Node(2,i,x+1,y+len+1,1);
			}
			if(dir==4)
			{
				a[++tot]=Node(3,i,x-len,y,1);
				if(x+1<=n)a[++tot]=Node(3,i,x+1,y,-1);
				if(y-1>=1)a[++tot]=Node(1,i,x-len,y-1,-1);
				if(x+1<=n&&y-len-1>=1)a[++tot]=Node(1,i,x+1,y-len-1,1);
			}
		}
		else
		{
			int x,y;
			scanf("%d%d",&x,&y);
			a[++tot]=Node(0,i,x,y,0);
			flag[i]=1;
		}
	}
//	for(int i=1;i<=tot;i++)
//	{
//		printf("%d:",i);
//		printf("type=%d,id=%d,(%d,%d),c=%d\n",a[i].type,a[i].id,a[i].x,a[i].y,a[i].c);
//	}
	solve(1,tot);
	for(int i=1;i<=q;i++)
	{
		if(flag[i])
		{
			printf("%d\n",ans[i]);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node
{
	int x,y;
	int type;
	int c;
	int id;
	Node(const int t=0,const int i=0,const int xx=0,const int yy=0,const int cc=0)
	{
		type=t,id=i;
		x=xx,y=yy,c=cc;
	}
}a[1000005],tmp[1000005];
int n,q,tot,ans[1000005],top,c[1000005],c0[1000005],c1[1000005];
bool flag[1000005];
void add(int p,int v,int*c)
{
	while(p<=2*n)
	{
		c[p]+=v;
		p+=p^(p&(p-1));
	}
}
int sum(int p,int*c)
{
	int ans=0;
	while(p)
	{
		ans+=c[p];
		p&=p-1;
	}
	return ans;
}
void solve(int l,int r)
{
	if(l==r)return;
	int mid=(l+r)>>1;
	solve(l,mid);
	solve(mid+1,r);
	top=0;
	int now1=l,now2=mid+1;
//	printf("[%d,%d]:\n",l,r);
	while(now1<=mid||now2<=r)
	{
		while(now1<=mid&&a[now1].type==0)now1++;
		while(now2<=r&&a[now2].type!=0)now2++;
		if(now1>mid&&now2>r)break;
		if(now1>mid)tmp[++top]=a[now2++];
		else if(now2>r)tmp[++top]=a[now1++];
		else
		{
			if(a[now1].x<=a[now2].x)tmp[++top]=a[now1++];
			else tmp[++top]=a[now2++];
		}
//		printf("type=%d,id=%d,(%d,%d),c=%d\n",tmp[top].type,tmp[top].id,tmp[top].x,tmp[top].y,tmp[top].c);
		if(tmp[top].type==1)
		{
			add(1,tmp[top].c,c0);
			add(tmp[top].x+tmp[top].y+1,-tmp[top].c,c0);
		}
		else if(tmp[top].type==2)
		{
			add(1,tmp[top].c,c1);
			add(tmp[top].x-tmp[top].y+n+1,-tmp[top].c,c1);
		}
		else if(tmp[top].type==3)
		{
			add(1,tmp[top].c,c);
			add(tmp[top].y+1,-tmp[top].c,c);
		}
		else if(tmp[top].type==4)
		{
			add(tmp[top].y,tmp[top].c,c);
		}
		else if(tmp[top].type==0)
			ans[tmp[top].id]+=sum(tmp[top].y,c)+sum(tmp[top].x+tmp[top].y,c0)+sum(tmp[top].x-tmp[top].y+n,c1);
	}
	for(int i=1;i<=top;i++)
	{
		if(tmp[i].type==1)
		{
			add(1,-tmp[i].c,c0);
			add(tmp[i].x+tmp[i].y+1,tmp[i].c,c0);
		}
		else if(tmp[i].type==2)
		{
			add(1,-tmp[i].c,c1);
			add(tmp[i].x-tmp[i].y+n+1,tmp[i].c,c1);
		}
		else if(tmp[i].type==3)
		{
			add(1,-tmp[i].c,c);
			add(tmp[i].y+1,tmp[i].c,c);
		}
		else if(tmp[i].type==4)
		{
			add(tmp[i].y,-tmp[i].c,c);
		}
	}
	top=0;
	now1=l,now2=mid+1;
	while(now1<=mid||now2<=r)
	{
		if(now1>mid)tmp[++top]=a[now2++];
		else if(now2>r)tmp[++top]=a[now1++];
		else
		{
			if(a[now1].x<=a[now2].x)tmp[++top]=a[now1++];
			else tmp[++top]=a[now2++];
		}
	}
	for(int i=1;i<=top;i++)
		a[l+i-1]=tmp[i];
}
int main()
{
	scanf("%d%d",&n,&q);
	for(int i=1;i<=q;i++)
	{
		int type;
		scanf("%d",&type);
		if(type==1)
		{
			int dir,x,y,len;
			scanf("%d%d%d%d",&dir,&x,&y,&len);
			if(dir==1)
			{
				a[++tot]=Node(1,i,x,y+len,1);
				if(x+len+1<=n&&y-1>=1)a[++tot]=Node(1,i,x+len+1,y-1,-1);
				if(y-1>=1)a[++tot]=Node(3,i,x,y-1,-1);
				if(x+len+1<=n&&y-1>=1)a[++tot]=Node(3,i,x+len+1,y-1,1);
			}
			if(dir==2)
			{
				a[++tot]=Node(2,i,x,y-len,1);
				if(x+len+1<=n&&y+1<=n)a[++tot]=Node(2,i,x+len+1,y+1,-1);
				if(y+1<=n)a[++tot]=Node(4,i,x,y+1,-1);
				if(x+len+1<=n&&y+1<=n)a[++tot]=Node(4,i,x+len+1,y+1,1);
			}
			if(dir==3)
			{
				a[++tot]=Node(4,i,x-len,y,1);
				if(x+1<=n)a[++tot]=Node(4,i,x+1,y,-1);
				if(y+1<=n)a[++tot]=Node(2,i,x-len,y+1,-1);
				if(x+1<=n&&y+len+2<=n)a[++tot]=Node(2,i,x+1,y+len+2,1);
			}
			if(dir==4)
			{
				a[++tot]=Node(3,i,x-len,y,1);
				if(x+1<=n)a[++tot]=Node(3,i,x+1,y,-1);
				if(y-1>=1)a[++tot]=Node(1,i,x-len,y-1,-1);
				if(x+1<=n&&y-len-2>=1)a[++tot]=Node(1,i,x+1,y-len-2,1);
			}
		}
		else
		{
			int x,y;
			scanf("%d%d",&x,&y);
			a[++tot]=Node(0,i,x,y,0);
			flag[i]=1;
		}
	}
//	for(int i=1;i<=tot;i++)
//	{
//		printf("%d:",i);
//		printf("type=%d,id=%d,(%d,%d),c=%d\n",a[i].type,a[i].id,a[i].x,a[i].y,a[i].c);
//	}
	solve(1,tot);
	for(int i=1;i<=q;i++)
	  if(flag[i])
		printf("%d ",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline void myassert(bool a,string s){ if(!a)cerr<<s<<endl,exit(0);}
inline int read()
{
	int s=0,f=1;char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const int maxn=5e3+20;

struct Query {
	int x,y,len,dir;
};
Query qu[100020];

int n,q;

int a[maxn][maxn],b[maxn][maxn<<1],c[maxn][maxn<<1];
int s[maxn<<1];

inline void init()
{
	n=read(); q=read();
	REP(i,1,q)
	{
		int op=read();
		if(op==1)
		{
			int dir=read(),x=read(),y=read(),len=read();
			qu[i]=(Query){x,y,len,dir};
		}else
		{
			int x=read(),y=read();
			qu[i]=(Query){x,y,0,5};
		}
	}
}

int ans[100020];

inline void adds(int x0,int d){ for(int x=x0;x<=(n<<1);x+=x&(-x))s[x]+=d;}
inline void addxy(int x0,int y0,int d){ for(int x=x0,y;x<=n;x+=x&(-x))for(y=y0;y<=n;y+=y&(-y))a[x][y]+=d;}
inline void addxxy(int x0,int y0,int d){ for(int x=x0,y;x<=n;x+=x&(-x))for(y=y0;y<=(n<<1);y+=y&(-y))b[x][y]+=d;}
inline void addxyy(int x0,int y0,int d){ for(int x=x0,y;x<=n;x+=x&(-x))for(y=y0;y<=(n<<1);y+=y&(-y))c[x][y]+=d;}

inline int sums(int x0){ int res=0; for(int x=x0;x;x-=x&(-x))res+=s[x]; return res;}
inline int sumxy(int x0,int y0){ int res=0; for(int x=x0;x;x-=x&(-x))for(int y=y0;y;y-=y&(-y))res+=a[x][y]; return res;}
inline int sumxxy(int x0,int y0){ int res=0; for(int x=x0;x;x-=x&(-x))for(int y=y0;y;y-=y&(-y))res+=b[x][y]; return res;}
inline int sumxyy(int x0,int y0){ int res=0; for(int x=x0;x;x-=x&(-x))for(int y=y0;y;y-=y&(-y))res+=c[x][y]; return res;}

inline void add(int x,int y,int len,int d)
{
	adds(x+y-len,d);
	addxy(x+1,y+1,d);
	addxxy(x+1,x+y-len,-d);
	addxyy(y+1,x+y-len,-d);
}

inline int sum(int x,int y)
{
	return sums(x+y)+sumxy(x,y)+sumxxy(x,x+y)+sumxyy(y,x+y);
}

inline void work(int op)
{
	REP(i,1,q)
	{
		int x=qu[i].x,y=qu[i].y;
		if(op==1 || op==3)y=n-y+1;
		if(op<=2)x=n-x+1;
		if(qu[i].dir==5)
		{
			ans[i]+=sum(x,y);
			//if(i==5)cerr<<ans[i]<<endl;
		}
		else if(qu[i].dir==op)
		{
			add(x,y,qu[i].len,1);
		}
	}
	REP(i,1,q)
	{
		int x=qu[i].x,y=qu[i].y;
		if(op==1 || op==3)y=n-y+1;
		if(op<=2)x=n-x+1;
		if(qu[i].dir==op)
		{
			add(x,y,qu[i].len,-1);
		}
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
#endif
	init();
	REP(i,1,4)work(i);
	REP(i,1,q)if(qu[i].dir>4)printf("%d\n",ans[i]);
	return 0;
}


#include <cstdio>

const int MN = 10005, MQ = 100005;

int N, Q;
int op[MQ], dir[MQ], x[MQ], y[MQ], c[MQ], Ans[MQ];

int Nx, Ny, bit[MN][MN], stkx[MQ], stky[MQ], stkz[MQ], tp;
inline void Add(int x, int y, int z) {
	++tp, stkx[tp] = x, stky[tp] = y, stkz[tp] = z;
	for (int i = x; i <= Nx; i += i & -i)
		for (int j = y; j <= Ny; j += j & -j)
			bit[i][j] += z;
}
inline int Qur(int x, int y) {
	int a = 0;
	for (int i = x; i; i -= i & -i)
		for (int j = y; j; j -= j & -j)
			a += bit[i][j];
	return a;
}
inline void clear() {
	while (tp) Add(stkx[tp], stky[tp], -stkz[tp]), tp -= 2;
}

int main() {
	scanf("%d%d", &N, &Q);
	for (int i = 1; i <= Q; ++i) {
		scanf("%d", &op[i]);
		if (op[i] == 1) scanf("%d%d%d%d", &dir[i], &x[i], &y[i], &c[i]);
		else scanf("%d%d", &x[i], &y[i]);
	}
	Nx = N, Ny = N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 1) Add(x[i], y[i], 1);
			if (dir[i] == 2) Add(x[i], y[i] + 1, -1);
			if (dir[i] == 3) Add(x[i] + 1, y[i], -1);
			if (dir[i] == 4) Add(x[i] + 1, y[i] + 1, 1);
		} else Ans[i] += Qur(x[i], y[i]);
	} clear();
	Nx = N + N, Ny = N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 1) Add(x[i] + y[i] + c[i] + 1, y[i], -1);
			if (dir[i] == 4) Add(x[i] + y[i] - c[i], y[i] + 1, -1);
		} else Ans[i] += Qur(x[i] + y[i], y[i]);
	} clear();
	Nx = N, Ny = N + N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 1) Add(N - x[i] + 2, x[i] + y[i] + c[i] + 1, 1);
			if (dir[i] == 4) Add(N - x[i] + 1, x[i] + y[i] - c[i], 1);
		} else Ans[i] += Qur(N - x[i] + 1, x[i] + y[i]);
	} clear();
	Nx = N + N, Ny = N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 2) Add(N + x[i] - y[i] + c[i] + 2, y[i] + 1, 1);
			if (dir[i] == 3) Add(N + x[i] - y[i] - c[i] + 1, y[i], 1);
		} else Ans[i] += Qur(N + x[i] - y[i] + 1, y[i]);
	} clear();
	Nx = N, Ny = N + N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 2) Add(x[i], N - x[i] + y[i] - c[i] + 1, 1);
			if (dir[i] == 3) Add(x[i] + 1, N - x[i] + y[i] + c[i] + 2, 1);
		} else Ans[i] += Qur(x[i], N - x[i] + y[i] + 1);
	} clear();
	for (int i = 1; i <= Q; ++i) if (op[i] == 2) printf("%d\n", Ans[i]);
	return 0;
}


#include <cstdio>

const int MN = 10005, MQ = 100005;

int N, Q;
int op[MQ], dir[MQ], x[MQ], y[MQ], c[MQ], Ans[MQ];

int Nx, Ny, bit[MN][MN], stkx[MQ], stky[MQ], stkz[MQ], tp;
inline void Add(int x, int y, int z) {
	++tp, stkx[tp] = x, stky[tp] = y, stkz[tp] = z;
	for (int i = x; i <= Nx; i += i & -i)
		for (int j = y; j <= Ny; j += j & -j)
			bit[i][j] += z;
}
inline int Qur(int x, int y) {
	int a = 0;
	for (int i = x; i; i -= i & -i)
		for (int j = y; j; j -= j & -j)
			a += bit[i][j];
	return a;
}
inline void clear() {
	while (tp) Add(stkx[tp], stky[tp], -stkz[tp]), tp -= 2;
}

int main() {
	scanf("%d%d", &N, &Q);
	for (int i = 1; i <= Q; ++i) {
		scanf("%d", &op[i]);
		if (op[i] == 1) scanf("%d%d%d%d", &dir[i], &x[i], &y[i], &c[i]);
		else scanf("%d%d", &x[i], &y[i]);
	}
	Nx = N, Ny = N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 1) Add(x[i], y[i], 1);
			if (dir[i] == 2) Add(x[i], y[i] + 1, -1);
			if (dir[i] == 3) Add(x[i] + 1, y[i], -1);
			if (dir[i] == 4) Add(x[i] + 1, y[i] + 1, 1);
		} else Ans[i] += Qur(x[i], y[i]);
	} clear();
	Nx = N + N, Ny = N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 1) Add(x[i] + y[i] + c[i] + 1, y[i], -1);
			if (dir[i] == 4) Add(x[i] + y[i] - c[i] - 1, y[i] + 1, -1);
		} else Ans[i] += Qur(x[i] + y[i], y[i]);
	} clear();
	Nx = N, Ny = N + N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 1) Add(N - x[i] + 2, x[i] + y[i] + c[i] + 1, 1);
			if (dir[i] == 4) Add(N - x[i] + 1, x[i] + y[i] - c[i] - 1, 1);
		} else Ans[i] += Qur(N - x[i] + 1, x[i] + y[i]);
	} clear();
	Nx = N + N, Ny = N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 2) Add(N + x[i] - y[i] + c[i] + 2, y[i] + 1, 1);
			if (dir[i] == 3) Add(N + x[i] - y[i] - c[i] + 1, y[i], 1);
		} else Ans[i] += Qur(N + x[i] - y[i] + 1, y[i]);
	} clear();
	Nx = N, Ny = N + N;
	for (int i = 1; i <= Q; ++i) {
		if (op[i] == 1) {
			if (dir[i] == 2) Add(x[i], N - x[i] + y[i] - c[i] + 1, 1);
			if (dir[i] == 3) Add(x[i] + 1, N - x[i] + y[i] + c[i] + 2, 1);
		} else Ans[i] += Qur(x[i], N - x[i] + y[i] + 1);
	} clear();
	for (int i = 1; i <= Q; ++i) if (op[i] == 2) printf("%d\n", Ans[i]);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
#include <tuple>
#define FILL(a, n, x) memset((a), (x), sizeof ((a)[0]) * (n))
#define FILLA(a, x) memset((a), (x), sizeof (a))
#define COPY(a, n, b) memcpy((b), (a), sizeof ((a)[0]) * (n))
#define COPYA(a, b) memcpy((b), (a), sizeof (a))
using namespace std;

typedef pair<int, int> pii;
typedef long long li;
typedef unsigned long long lu;
const int inf = ~0U >> 2;
const li infl = ~0LLU >> 2;

template <class T> inline void Min(T &x, T y) { if (y < x) x = y; }
template <class T> inline void Max(T &x, T y) { if (y > x) x = y; }

const int mod = 998244353;
inline int Add(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
inline int Sub(int x) { return x < 0 ? x + mod : x; }
inline void Sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
inline int Mul(int x, int y) { return (int)((li)x * y % mod); }
inline int Mul(int x, int y, int z) { return Mul(x, Mul(y, z)); }
int Pow(int x, int y) { int z = 1;
  for (; y; y >>= 1, x = Mul(x, x)) { if (y & 1) z = Mul(z, x); } return z; }
inline int Inv(int x) { return Pow(x, mod - 2); }
inline int Fix(li x) { x %= mod; if (x < 0) x += mod; return (int)x; }
inline int Fix(int x) { x %= mod; if (x < 0) x += mod; return x; }

// ----------------------------------------

const int maxn = 5000;
const int maxq = 1e5;

int n, m, q;

struct Query {
  int type, x, y, para;
} qry[maxq];

int bit[maxn * 2][maxn];
int W, H;

int Low(int i) {
  return i & -i;
}

void Init(int w, int h) {
  W = w;
  H = h;
  FILL(bit, W, 0);
}

void AAdd(int X, int Y, int v) {
  for (int x = X; x < W; x += Low(x + 1)) {
    for (int y = Y; y < H; y += Low(y + 1)) {
      bit[x][y] += v;
    }
  }
}

int SSum(int X, int Y) {
  int sum = 0;
  for (int x = X; x >= 0; x -= Low(x + 1)) {
    for (int y = Y; y >= 0; y -= Low(y + 1)) {
      sum += bit[x][y];
    }
  }
  return sum;
}

void Tran(int &x, int &y) {
  x += n - y;
}

void Solve(int type) {
  // part easy
  Init(n, n);
  for (int i = 0; i < q; ++i) {
    if (qry[i].type == type) {
      int x0 = qry[i].x, y0 = qry[i].y, len = qry[i].para;
      int x2 = x0, y2 = y0 + len;
      AAdd(x0 + 1, y0, -1);
      AAdd(x2 + 1, y2 + 1, 1);
    } else if (qry[i].type == -1) {
      int x = qry[i].x, y = qry[i].y, &ans = qry[i].para;
      ans += SSum(x, y);
    }
  }

  // part hard
  Init(2 * n, n);
  for (int i = 0; i < q; ++i) {
    if (qry[i].type == type) {
      int x0 = qry[i].x, y0 = qry[i].y, len = qry[i].para;
      int x1 = x0 - len, y1 = y0;
      int x2 = x0, y2 = y0 + len;
      Tran(x0, y0);
      Tran(x1, y1);
      Tran(x2, y2);
      AAdd(x1, y1, 1);
      AAdd(x2, y2 + 1, -1);
    } else if (qry[i].type == -1) {
      int x = qry[i].x, y = qry[i].y, &ans = qry[i].para;
      Tran(x, y);
      ans += SSum(x, y);
    }
  }
}

void Rotate(void) {
  for (int i = 0; i < q; ++i) {
    int &x = qry[i].x, &y = qry[i].y;
    tie(x, y) = make_pair(n - 1 - y, x);
  }
}

int main(void) {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < q; ++i) {
    int type;
    scanf("%d", &type);
    if (type == 1) {
      int dir, x, y, len;
      scanf("%d%d%d%d", &dir, &x, &y, &len);
      --x; --y;
      qry[i] = {dir, x, y, len};
    } else {
      int x, y;
      scanf("%d%d", &x, &y);
      --x; --y;
      qry[i] = {-1, x, y, 0};
    }
  }
  Solve(3);
  Rotate();
  Solve(1);
  Rotate();
  Solve(2);
  Rotate();
  Solve(4);
  for (int i = 0; i < q; ++i) {
    if (qry[i].type == -1) {
      printf("%d\n", qry[i].para);
    }
  }
  return 0;
}


#include <algorithm>
// #include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <set>
#include <map>
// #include <bitset>
using namespace std;
typedef long long LL;
typedef double db;
const int oo=2139063143;
const int N=100010;
const int P=1000000007;
const db eps=1e-7;
#define pritnf printf
//char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T>
inline void sc (T &x)
{
    x=0; static int p; p=1; static char c; c=getchar();
    while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); }
    while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); }
    x*=p;
}
template <typename T>
inline void print (T x)
{
    if (x< 0) putchar('-'),x=-x;
    if (x>=10) print(x/10);
    putchar(x%10+'0');
}
template <typename T>
inline void pr (T x) { print(x),putchar('\n'); }
template <typename T1,typename T2>
void chkmax (T1 &A,T2 B) { A=A>=B?A:B; }
template <typename T1,typename T2>
void chkmin (T1 &A,T2 B) { A=A<=B?A:B; }
int G,H;
int b[10400][10400];
void add0 (int x,int y,int d)
{
	for (int i=x; i<=H; i+=i&-i)
		for (int j=y; j<=H; j+=j&-j)
			b[i][j]+=d;
}
int sum (int x,int y)
{
	int ans=0; ++x,++y;
	for (int i=x; i; i-=i&-i)
		for (int j=y; j; j-=j&-j)
			ans+=b[i][j];
	return ans;
}
int sx[N],sy[N],sd[N],z=0;
void add (int x,int y,int d)
{
	++x,++y;
	sx[++z]=x,sy[z]=y,sd[z]=d;
	add0 (x,y,d);
}
void clear () { while (z) add0 (sx[z],sy[z],-sd[z]),--z; }
int op[N],x[N],y[N],c[N],ans[N];
int main ()
{
    // freopen (".in","r",stdin);
    // freopen (".out","w",stdout);
	int n; sc(G),sc(n); ++G; H=G<<1; H+=10;
	for (int i=1; i<=n; i++)
	{
		sc(op[i]);
		if (op[i]==1) sc(op[i]),sc(x[i]),sc(y[i]),sc(c[i]);
		else op[i]=0,sc(x[i]),sc(y[i]);
	}
	for (int i=1; i<=n; i++)
		if (op[i]==1) add (x[i],y[i],1);
		else if (!op[i]) ans[i]+=sum (x[i],y[i]);
	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==1) add (x[i]+y[i]+c[i]+1,x[i],-1);
		else if (!op[i]) ans[i]+=sum (x[i]+y[i],x[i]);
	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==1) add (x[i]+y[i]+c[i]+1,G-y[i]+1,1);
		else if (!op[i]) ans[i]+=sum (x[i]+y[i],G-y[i]);

	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==2) add (x[i],G-y[i],1);
		else if (!op[i]) ans[i]+=sum (x[i],G-y[i]);
	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==2) add (x[i]-y[i]+c[i]+G+1,x[i],-1);
		else if (!op[i]) ans[i]+=sum (x[i]-y[i]+G,x[i]);
	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==2) add (x[i]-y[i]+c[i]+G+1,y[i]+1,1);
		else if (!op[i]) ans[i]+=sum (x[i]-y[i]+G,y[i]);

	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==3) add (G-x[i],y[i],1);
		else if (!op[i]) ans[i]+=sum (G-x[i],y[i]);
	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==3) add (G-(x[i]-y[i]-c[i])+1,G-x[i],-1);
		else if (!op[i]) ans[i]+=sum (G-(x[i]-y[i]),G-x[i]);
	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==3) add (G-(x[i]-y[i]-c[i])+1,G-y[i]+1,1);
		else if (!op[i]) ans[i]+=sum (G-(x[i]-y[i]),G-y[i]);

	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==4) add (G-x[i],G-y[i],1);
		else if (!op[i]) ans[i]+=sum (G-x[i],G-y[i]);
	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==4) add (G-(x[i]+y[i]-c[i])+G+1,G-x[i],-1);
		else if (!op[i]) ans[i]+=sum (G-(x[i]+y[i])+G,G-x[i]);
	clear ();
	for (int i=1; i<=n; i++)
		if (op[i]==4) add (G-(x[i]+y[i]-c[i])+G+1,y[i]+1,1);
		else if (!op[i]) ans[i]+=sum (G-(x[i]+y[i])+G,y[i]);

	for (int i=1; i<=n; i++)
		if (!op[i]) pr(ans[i]);

    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=(0);i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define clz __builtin_clz
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=5005,M=100005;
int op[M],d[M],x[M],y[M],len[M],ans[M],n,Q;
struct bit{
	int a[N][N*2];
	void clear(){
		memset(a,0,sizeof a);
	}
	#define lb(x) (x&(-x))
	void upd(int x,int y,int z){
		for(int i=x;i;i-=lb(i))
			for(int j=y;j;j-=lb(j))
				a[i][j]+=z;
	}
	void upd(int l,int r,int y,int z){
		l=max(l,1),r=min(r,n);
		y=max(y,0),y=min(y,2*n);
		upd(l-1,y,-z),upd(r,y,z);
	}
	int qry(int x,int y){
		int res=0;
		for(int i=x;i<=n;i+=lb(i))
			for(int j=y;j<=n*2;j+=lb(j))
				res+=a[i][j];
		return res;
	}
}A,B;

int main(){
	read(n),read(Q);
	rep(i,1,Q){
		read(op[i]);
		if(op[i]==1){
			read(d[i]),read(x[i]);
			read(y[i]),read(len[i]);
		}
		else{
			read(x[i]),read(y[i]);
		}
	}
	rep(i,1,Q)
		if(op[i]==1&&d[i]==1){
			A.upd(x[i],x[i]+len[i],y[i]-1,-1);
			B.upd(x[i],x[i]+len[i],x[i]+y[i]+len[i],1);
		}
		else if(op[i]==1&&d[i]==4){
			A.upd(x[i]-len[i],x[i],y[i],1);
			B.upd(x[i]-len[i],x[i],x[i]+y[i]-len[i]-1,-1);
		}
		else if(op[i]==2){
			ans[i]+=A.qry(x[i],y[i]);
			ans[i]+=B.qry(x[i],x[i]+y[i]);
		}
	A.clear(),B.clear();
	rep(i,1,Q)
		if(op[i]==1&&d[i]==2){
			A.upd(x[i],x[i]+len[i],y[i],1);
			B.upd(x[i],x[i]+len[i],y[i]+(n+1-x[i])-len[i]-1,-1);
		}
		else if(op[i]==1&&d[i]==3){
			A.upd(x[i]-len[i],x[i],y[i]-1,-1);
			B.upd(x[i]-len[i],x[i],y[i]+(n+1-x[i])+len[i],1);
		}
		else if(op[i]==2){
			ans[i]+=A.qry(x[i],y[i]);
			ans[i]+=B.qry(x[i],y[i]+(n+1-x[i]));
		}
	rep(i,1,Q)
		if(op[i]==2) printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=(0);i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define clz __builtin_clz
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=5005,M=100005;
int op[M],d[M],x[M],y[M],len[M],ans[M],n,Q;
struct bit{
	int a[N][N*2];
	#define lb(x) (x&(-x))
	void upd(int x,int y,int z){
		for(int i=x;i;i-=lb(i))
			for(int j=y;j;j-=lb(j))
				a[i][j]+=z;
	}
	void upd(int l,int r,int y,int z){
		l=max(l,1),r=min(r,n);
		y=max(y,0),y=min(y,2*n);
		upd(l-1,y,-z),upd(r,y,z);
	}
	int qry(int x,int y){
		int res=0;
		for(int i=x;i<=n;i+=lb(i))
			for(int j=y;j<=n*2;j+=lb(j))
				res+=a[i][j];
		return res;
	}
}A,B;

int main(){
	read(n),read(Q);
	rep(i,1,Q){
		read(op[i]);
		if(op[i]==1){
			read(d[i]),read(x[i]);
			read(y[i]),read(len[i]);
		}
		else{
			read(x[i]),read(y[i]);
		}
	}
	rep(i,1,Q)
		if(op[i]==1&&d[i]==1){
			A.upd(x[i],x[i]+len[i],y[i]-1,-1);
			B.upd(x[i],x[i]+len[i],x[i]+y[i]+len[i],1);
		}
		else if(op[i]==1&&d[i]==4){
			A.upd(x[i]-len[i],x[i],y[i],1);
			B.upd(x[i]-len[i],x[i],x[i]+y[i]-len[i]-1,-1);
		}
		else if(op[i]==2){
			ans[i]+=A.qry(x[i],y[i]);
			ans[i]+=B.qry(x[i],x[i]+y[i]);
		}
	rep(i,1,Q)
		if(op[i]==1&&d[i]==1){
			A.upd(x[i],x[i]+len[i],y[i]-1,1);
			B.upd(x[i],x[i]+len[i],x[i]+y[i]+len[i],-1);
		}
		else if(op[i]==1&&d[i]==4){
			A.upd(x[i]-len[i],x[i],y[i],-1);
			B.upd(x[i]-len[i],x[i],x[i]+y[i]-len[i]-1,1);
		}
	rep(i,1,Q)
		if(op[i]==1&&d[i]==2){
			A.upd(x[i],x[i]+len[i],y[i],1);
			B.upd(x[i],x[i]+len[i],y[i]+(n+1-x[i])-len[i]-1,-1);
		}
		else if(op[i]==1&&d[i]==3){
			A.upd(x[i]-len[i],x[i],y[i]-1,-1);
			B.upd(x[i]-len[i],x[i],y[i]+(n+1-x[i])+len[i],1);
		}
		else if(op[i]==2){
			ans[i]+=A.qry(x[i],y[i]);
			ans[i]+=B.qry(x[i],y[i]+(n+1-x[i]));
		}
	rep(i,1,Q)
		if(op[i]==2) printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=(0);i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define clz __builtin_clz
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=5005,M=100005;
int op[M],d[M],x[M],y[M],len[M],ans[M],n,Q;
struct bit{
	int a[N][N*2];
	void clear(){
		memset(a,0,sizeof a);
	}
	#define lb(x) (x&(-x))
	void upd(int x,int y,int z){
		for(int i=x;i;i-=lb(i))
			for(int j=y;j;j-=lb(j))
				a[i][j]+=z;
	}
	void upd(int l,int r,int y,int z){
		l=max(l,1),r=min(r,n);
		y=max(y,1),y=min(y,2*n);
		upd(l-1,y,-z),upd(r,y,z);
	}
	int qry(int x,int y){
		int res=0;
		for(int i=x;i<=n;i+=lb(i))
			for(int j=y;j<=n*2;j+=lb(j))
				res+=a[i][j];
		return res;
	}
}A,B;

int main(){
	read(n),read(Q);
	rep(i,1,Q){
		read(op[i]);
		if(op[i]==1){
			read(d[i]),read(x[i]);
			read(y[i]),read(len[i]);
		}
		else{
			read(x[i]),read(y[i]);
		}
	}
	rep(i,1,Q)
		if(op[i]==1&&d[i]==1){
			A.upd(x[i],x[i]+len[i],y[i]-1,-1);
			B.upd(x[i],x[i]+len[i],x[i]+y[i]+len[i],1);
		}
		else if(op[i]==1&&d[i]==4){
			A.upd(x[i]-len[i],x[i],y[i],1);
			B.upd(x[i]-len[i],x[i],x[i]+y[i]-len[i]-1,-1);
		}
		else if(op[i]==2){
			ans[i]+=A.qry(x[i],y[i]);
			ans[i]+=B.qry(x[i],x[i]+y[i]);
		}
	A.clear(),B.clear();
	rep(i,1,Q)
		if(op[i]==1&&d[i]==2){
			A.upd(x[i],x[i]+len[i],y[i],1);
			B.upd(x[i],x[i]+len[i],y[i]+(n+1-x[i])-len[i]-1,-1);
		}
		else if(op[i]==1&&d[i]==3){
			A.upd(x[i]-len[i],x[i],y[i]-1,-1);
			B.upd(x[i]-len[i],x[i],y[i]+(n+1-x[i])+len[i],1);
		}
		else if(op[i]==2){
			ans[i]+=A.qry(x[i],y[i]);
			ans[i]+=B.qry(x[i],y[i]+(n+1-x[i]));
		}
	rep(i,1,Q)
		if(op[i]==2) printf("%d\n",ans[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 5050;
const int TT = 100010;
const int TT6 = (TT >> 6) + 10;

unsigned long long one = 1, mx[N][TT6], my[N][TT6], mz[N + N][TT6];
vector < pair <int, int> > ex[N], ey[N], ez[N + N];

int com[TT], dir[TT], x[TT], y[TT], len[TT], res[TT];

int kb[1 << 16];

inline int my_popcountll(unsigned long long x) {
  int res = kb[x & 65535];
  x >>= 16;
  res += kb[x & 65535];
  x >>= 16;
  res += kb[x & 65535];
  x >>= 16;
  res += kb[x];
  return res;
}

int main() {
  kb[0] = 0;
  for (int i = 1; i < (1 << 16); i++) {
    kb[i] = kb[i & (i - 1)] + 1;
  }
  int sz, tt;
  scanf("%d %d", &sz, &tt);
  int tt6 = ((tt - 1) >> 6) + 1;
  for (int qq = 0; qq < tt; qq++) {
    scanf("%d", com + qq);
    if (com[qq] == 1) {
      scanf("%d %d %d %d", dir + qq, x + qq, y + qq, len + qq);
    } else {
      scanf("%d %d", x + qq, y + qq);
      res[qq] = 0;
    }
  }
  for (int rot = 0; rot < 2; rot++) {
    for (int i = 0; i <= sz; i++) {
      ex[i].clear();
      ey[i].clear();
      for (int j = 0; j < tt6; j++) {
        mx[i][j] = 0;
        my[i][j] = 0;
      }
    }
    for (int i = 0; i <= sz + sz; i++) {
      ez[i].clear();
      for (int j = 0; j < tt6; j++) {
        mz[i][j] = 0;
      }
    }
    for (int qq = 0; qq < tt; qq++) {
      if (com[qq] == 1) {
        if (dir[qq] == 1) {
          ex[x[qq]].push_back(make_pair(qq, 1));
          ey[y[qq]].push_back(make_pair(qq, 1));
          mz[0][qq >> 6] |= (one << (qq & 63));
          ez[x[qq] + y[qq] + len[qq] + 1].push_back(make_pair(qq, 0));
        }
        if (dir[qq] == 4) {
          mx[0][qq >> 6] |= (one << (qq & 63));
          ex[x[qq] + 1].push_back(make_pair(qq, 0));
          my[0][qq >> 6] |= (one << (qq & 63));
          ey[y[qq] + 1].push_back(make_pair(qq, 0));
          ez[x[qq] + y[qq] - len[qq]].push_back(make_pair(qq, 1));
        }
      }
    }
    for (int xx = 1; xx <= sz; xx++) {
      for (int j = 0; j < tt6; j++) {
        mx[xx][j] |= mx[xx - 1][j];
      }
      int sz = ex[xx].size();
      for (int q = 0; q < sz; q++) {
        int u = ex[xx][q].first;
        int v = ex[xx][q].second;
        if (v == 1) {
          mx[xx][u >> 6] |= (one << (u & 63));
        } else {
          mx[xx][u >> 6] &= (~(one << (u & 63)));
        }
      }
    }
    for (int yy = 1; yy <= sz; yy++) {
      for (int j = 0; j < tt6; j++) {
        my[yy][j] |= my[yy - 1][j];
      }
      int sz = ey[yy].size();
      for (int q = 0; q < sz; q++) {
        int u = ey[yy][q].first;
        int v = ey[yy][q].second;
        if (v == 1) {
          my[yy][u >> 6] |= (one << (u & 63));
        } else {
          my[yy][u >> 6] &= (~(one << (u & 63)));
        }
      }
    }
    for (int zz = 1; zz <= sz + sz; zz++) {
      for (int j = 0; j < tt6; j++) {
        mz[zz][j] |= mz[zz - 1][j];
      }
      int sz = ez[zz].size();
      for (int q = 0; q < sz; q++) {
        int u = ez[zz][q].first;
        int v = ez[zz][q].second;
        if (v == 1) {
          mz[zz][u >> 6] |= (one << (u & 63));
        } else {
          mz[zz][u >> 6] &= (~(one << (u & 63)));
        }
      }
    }
    for (int qq = 0; qq < tt; qq++) {
      if (com[qq] == 2) {
        int z = x[qq] + y[qq];
        int up = (qq >> 6);
        for (int j = 0; j < up; j++) {
          unsigned long long r = mx[x[qq]][j] & my[y[qq]][j] & mz[z][j];
          res[qq] += my_popcountll(r);
        }
        unsigned long long r = mx[x[qq]][up] & my[y[qq]][up] & mz[z][up];
        r &= ((one << (qq & 63)) - 1);
        res[qq] += my_popcountll(r);
      }
    }
    for (int qq = 0; qq < tt; qq++) {
      y[qq] = sz - y[qq] + 1;
      if (dir[qq] <= 2) {
        dir[qq] = 3 - dir[qq];
      } else {
        dir[qq] = 7 - dir[qq];
      }
    }
  }
  for (int qq = 0; qq < tt; qq++) {
    if (com[qq] == 2) {
      printf("%d\n", res[qq]);
    }
  }
  return 0;
}


#include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 1e5 + 50, M = 5e3 + 50;
int n, m, as[N];
struct node{ int t, vl, coe; };
namespace BIT{
	int c[N];
	void mdf(int x, int v){ for(;x<=m;x+=x&-x) c[x]+=v; }
	int ask(int x){ int as=0; for(;x;x-=x&-x) as+=c[x]; return as; }
}
namespace SGT{
	cs int N = ::M << 2;
	#define mid ((l+r)>>1)
	vector<node> mdf[3][N];
	void ins(int x, int l, int r, int L, int R, int op, node now){
		if(L<=l&&r<=R){ mdf[op][x].pb(now); return; }
		if(L<=mid) ins(x<<1,l,mid,L,R,op,now);
		if(R>mid) ins(x<<1|1,mid+1,r,L,R,op,now);
	}
	void qry(int u, int l, int r, int p, int t, int x, int y){
		mdf[0][u].pb({t,y,0});
		mdf[1][u].pb({t,x+y,0});
		mdf[2][u].pb({t,y-x,0}); if(l==r) return;
		if(p<=mid) qry(u<<1,l,mid,p,t,x,y);
		else qry(u<<1|1,mid+1,r,p,t,x,y);
	}
	void subwork(vector<node> &A){
		if(!A.size()) return;
		sort(A.begin(),A.end(),[](cs node &a, cs node &b){
			if(a.vl == b.vl) return abs(a.coe) > abs(b.coe);
			return a.vl > b.vl;
		});
		for(auto a : A){
			if(a.coe) BIT::mdf(a.t,a.coe);
			else as[a.t] += BIT::ask(a.t);
		}
		for(auto a : A) if(a.coe) BIT::mdf(a.t,-a.coe);
	}
	void work(int u, int l, int r){
		subwork(mdf[0][u]);
		subwork(mdf[1][u]);
		subwork(mdf[2][u]); if(l==r) return;
		work(u<<1,l,mid); work(u<<1|1,mid+1,r);
	}
}
int main(){
	#ifdef FSYolanda
	freopen("1.in","r",stdin);
	#endif
	scanf("%d%d",&n,&m); static bool vs[N];
	for(int i=1,op,dir,x,y,len; i<=m; i++){
		scanf("%d",&op);
		if(op==1){
			scanf("%d%d%d%d",&dir,&x,&y,&len);
			if(dir==1){
			SGT::ins(1,1,n,x,x+len,1,{i,x+y+len,1});
			SGT::ins(1,1,n,x,x+len,0,{i,y-1,-1});
			} if(dir==2){
			SGT::ins(1,1,n,x,x+len,0,{i,y,1});
			SGT::ins(1,1,n,x,x+len,2,{i,y-len-1-x,-1});
			} if(dir==3){
			SGT::ins(1,1,n,x-len,x,2,{i,y+len-x,1});
			SGT::ins(1,1,n,x-len,x,0,{i,y-1,-1});
			} if(dir==4){
			SGT::ins(1,1,n,x-len,x,0,{i,y,1});
			SGT::ins(1,1,n,x-len,x,1,{i,x+y-len-1,-1});
			}
		}
		if(op==2){
			scanf("%d%d",&x,&y); vs[i]=true;
			SGT::qry(1,1,n,x,i,x,y);
		}
	} SGT::work(1,1,n);
	for(int i=1; i<=m; i++) if(vs[i]) cout<<as[i]<<'\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=5005;
int n,c1[Maxn][Maxn],c2[Maxn][2*Maxn],c3[Maxn][2*Maxn];
int lowbit(int x){return x&-x;}
template<class T>
void Add(int x,int y,int val,T c){
    for(int i=x;i<=n;i+=lowbit(i))
        for(int j=y;j<=((void*)c==c1?n:2*n);j+=lowbit(j))
            c[i][j]+=val;
}
template<class T>
int Ask(int x,int y,T c){
    // string qwq="";
    // if(c==c1)qwq="c1";
    // if(c==c2)qwq="c2";
    // if(c==c3)qwq="c3";
    // printf("query:%s:(%d,%d)\n",qwq.c_str(),x,y);
    // return c[x][y];
    int ans=0;
    for(int i=x;i;i-=lowbit(i))
        for(int j=y;j;j-=lowbit(j))
            ans+=c[i][j];
    return ans;
}
template<class T>
void Add(int x1,int y1,int x2,int y2,int val,T c){
    // string qwq="";
    // if(c==c1)qwq="c1";
    // if(c==c2)qwq="c2";
    // if(c==c3)qwq="c3";
    // printf("Add:%s((%d,%d),(%d,%d), val=%d)\n",qwq.c_str(),x1,y1,x2,y2,val);
    // for(int i=x1;i<=x2;i++)
    //     for(int j=y1;j<=y2;j++)
    //         c[i][j]+=val;
    // return;
    Add(x1,y1,val,c);
    Add(x2+1,y1,-val,c);
    Add(x1,y2+1,-val,c);
    Add(x2+1,y2+1,val,c);
    // Add(x2,y2,val,c);
    // Add(x1-1,y2,-val,c);
    // Add(x2,y1-1,-val,c);
    // Add(x1-1,y1-1,val,c);
}
int main(){
    int q;scanf("%d%d",&n,&q);
    while(q--){
        int type;scanf("%d",&type);
        if(type==1){
            int d,x,y,l;scanf("%d%d%d%d",&d,&x,&y,&l);
            if(d==1)Add(x,y,x+l,n,1,c1),Add(x,x+l+y+1,x+l,2*n,-1,c2);
            if(d==2)Add(x,y-l,n,y,1,c1),Add(y-l,x+1-y+l+n,y,2*n,-1,c3);
            if(d==3)Add(x+1,y,n,y+l,-1,c1),Add(y,x-l-y+n,y+l,2*n,1,c3);
            if(d==4)Add(x-l,y+1,x,n,-1,c1),Add(x-l,x+y-l,x,2*n,1,c2);
        }else{
            int x,y;scanf("%d%d",&x,&y);
            cout<<Ask(x,y,c1)+Ask(x,x+y,c2)+Ask(y,x-y+n,c3)<<"\n";
        }
    }
    return 0;
}
/*
17 2
1 3 10 3 7
2 4 4
*/


#include <bits/stdc++.h>
using namespace std;
const int Maxn=5005;
int n,c1[Maxn][2*Maxn],c2[Maxn][2*Maxn],c3[2*Maxn][2*Maxn];
int lowbit(int x){return x&-x;}
void Add(int x,int y,int val,int c[][2*Maxn]){
    for(int i=x;i<=n;i+=lowbit(i))
        for(int j=y;j<=2*n;j+=lowbit(j))
            c[i][j]+=val;
}
int Ask(int x,int y,int c[][2*Maxn]){
    // string qwq="";
    // if(c==c1)qwq="c1";
    // if(c==c2)qwq="c2";
    // if(c==c3)qwq="c3";
    // printf("query:%s:(%d,%d)\n",qwq.c_str(),x,y);
    // return c[x][y];
    int ans=0;
    for(int i=x;i;i-=lowbit(i))
        for(int j=y;j;j-=lowbit(j))
            ans+=c[i][j];
    return ans;
}
void Add(int x1,int y1,int x2,int y2,int val,int c[][2*Maxn]){
    // string qwq="";
    // if(c==c1)qwq="c1";
    // if(c==c2)qwq="c2";
    // if(c==c3)qwq="c3";
    // printf("Add:%s((%d,%d),(%d,%d), val=%d)\n",qwq.c_str(),x1,y1,x2,y2,val);
    // for(int i=x1;i<=x2;i++)
    //     for(int j=y1;j<=y2;j++)
    //         c[i][j]+=val;
    // return;
    Add(x1,y1,val,c);
    Add(x2+1,y1,-val,c);
    Add(x1,y2+1,-val,c);
    Add(x2+1,y2+1,val,c);
    // Add(x2,y2,val,c);
    // Add(x1-1,y2,-val,c);
    // Add(x2,y1-1,-val,c);
    // Add(x1-1,y1-1,val,c);
}
int main(){
    int q;scanf("%d%d",&n,&q);
    while(q--){
        int type;scanf("%d",&type);
        if(type==1){
            int d,x,y,l;scanf("%d%d%d%d",&d,&x,&y,&l);
            if(d==1)Add(x,y,x+l,n,1,c1),Add(x,x+l+y+1,x+l,2*n,-1,c2);
            if(d==2)Add(x,y-l,n,y,1,c1),Add(y-l,x+1-y+l+n,y,2*n,-1,c3);
            if(d==3)Add(x+1,y,n,y+l,-1,c1),Add(y,x-l-y+n,y+l,2*n,1,c3);
            if(d==4)Add(x-l,y+1,x,n,-1,c1),Add(x-l,x+y-l,x,2*n,1,c2);
        }else{
            int x,y;scanf("%d%d",&x,&y);
            cout<<Ask(x,y,c1)+Ask(x,x+y,c2)+Ask(y,x-y+n,c3)<<"\n";
        }
    }
    return 0;
}
/*
17 2
1 3 10 3 7
2 4 4
*/


#include <bits/stdc++.h>
using namespace std;
const int Maxn=5005;
int n,c1[Maxn][Maxn],c2[Maxn][2*Maxn],c3[2*Maxn][2*Maxn];
int lowbit(int x){return x&-x;}
template<class T>
void Add(int x,int y,int val,T c){
    for(int i=x;i<=n;i+=lowbit(i))
        for(int j=y;j<=((void*)c==c1?n:2*n);j+=lowbit(j))
            c[i][j]+=val;
}
template<class T>
int Ask(int x,int y,T c){
    // string qwq="";
    // if(c==c1)qwq="c1";
    // if(c==c2)qwq="c2";
    // if(c==c3)qwq="c3";
    // printf("query:%s:(%d,%d)\n",qwq.c_str(),x,y);
    // return c[x][y];
    int ans=0;
    for(int i=x;i;i-=lowbit(i))
        for(int j=y;j;j-=lowbit(j))
            ans+=c[i][j];
    return ans;
}
template<class T>
void Add(int x1,int y1,int x2,int y2,int val,T c){
    // string qwq="";
    // if(c==c1)qwq="c1";
    // if(c==c2)qwq="c2";
    // if(c==c3)qwq="c3";
    // printf("Add:%s((%d,%d),(%d,%d), val=%d)\n",qwq.c_str(),x1,y1,x2,y2,val);
    // for(int i=x1;i<=x2;i++)
    //     for(int j=y1;j<=y2;j++)
    //         c[i][j]+=val;
    // return;
    Add(x1,y1,val,c);
    Add(x2+1,y1,-val,c);
    Add(x1,y2+1,-val,c);
    Add(x2+1,y2+1,val,c);
    // Add(x2,y2,val,c);
    // Add(x1-1,y2,-val,c);
    // Add(x2,y1-1,-val,c);
    // Add(x1-1,y1-1,val,c);
}
int main(){
    int q;scanf("%d%d",&n,&q);
    while(q--){
        int type;scanf("%d",&type);
        if(type==1){
            int d,x,y,l;scanf("%d%d%d%d",&d,&x,&y,&l);
            if(d==1)Add(x,y,x+l,n,1,c1),Add(x,x+l+y+1,x+l,2*n,-1,c2);
            if(d==2)Add(x,y-l,n,y,1,c1),Add(y-l,x+1-y+l+n,y,2*n,-1,c3);
            if(d==3)Add(x+1,y,n,y+l,-1,c1),Add(y,x-l-y+n,y+l,2*n,1,c3);
            if(d==4)Add(x-l,y+1,x,n,-1,c1),Add(x-l,x+y-l,x,2*n,1,c2);
        }else{
            int x,y;scanf("%d%d",&x,&y);
            cout<<Ask(x,y,c1)+Ask(x,x+y,c2)+Ask(y,x-y+n,c3)<<"\n";
        }
    }
    return 0;
}
/*
17 2
1 3 10 3 7
2 4 4
*/


#include <bits/stdc++.h>
using namespace std;
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define D_INF numeric_limits<double>::infinity()
#define MIN(a, b) ((a) = min((a), (b)))
#define MAX(a, b) ((a) = max((a), (b)))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define all(a) (a).begin(), (a).end()
#define For(i, a, b) for (auto i = (a); i < (b); i++)
#define FOR(i, b) For(i, 0, b)
#define Rev(i, a, b) for (auto i = (a); i > (b); i--)
#define REV(i, a) Rev(i, a, -1)
#define sz(a) ((int) (a).size())
#define nl '\n'
#define sp ' '
#define ll long long
#define ld long double
#define uint unsigned int
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pill pair<int, ll>
#define plli pair<ll, int>
#define pdd pair<double, double>
#define uset unordered_set
#define umap unordered_map
#define pq priority_queue
template<typename T> using minpq = pq<T, vector<T>, greater<T>>;
template<typename T> using maxpq = pq<T, vector<T>, less<T>>;
template<typename T1,typename T2,typename H1=hash<T1>,typename H2=hash<T2>>struct pair_hash{size_t operator()(const pair<T1,T2>&p)const{return 31*H1{}(p.first)+H2{}(p.second);}};

#define _bufferSize 4096
#define _maxNumLength 128
char _inputBuffer[_bufferSize+1],*_inputPtr=_inputBuffer,_outputBuffer[_bufferSize],_c,_sign,*_tempInputBuf=nullptr,_numBuf[_maxNumLength],_tempOutputBuf[_maxNumLength],_fill=' ';
const char*_delimiter=" ";int _cur,_outputPtr=0,_numPtr=0,_precision=6,_width=0,_tempOutputPtr=0,_cnt;ull _precisionBase=1000000;
#define _peekchar() (*_inputPtr?*_inputPtr:(_inputBuffer[fread(_inputPtr=_inputBuffer,1,_bufferSize,stdin)]='\0',*_inputPtr))
#define _getchar() (*_inputPtr?*_inputPtr++:(_inputBuffer[fread(_inputPtr=_inputBuffer,1,_bufferSize,stdin)]='\0',*_inputPtr++))
#define _hasNext() (*_inputPtr||!feof(stdin))
#define _readSignAndNum(x) do{(x)=_getchar();}while((x)<=' ');_sign=(x)=='-';if(_sign)(x)=_getchar();for((x)-='0';(_c=_getchar())>='0';(x)=(x)*10+_c-'0')
#define _readFloatingPoint(x,T) for(T _div=1.0;(_c=_getchar())>='0';(x)+=(_c-'0')/(_div*=10))
#define rc(x) do{do{(x)=_getchar();}while((x)<=' ');}while(0)
#define ri(x) do{_readSignAndNum(x);if(_sign)(x)=-(x);}while(0)
#define rd(x) do{_readSignAndNum(x);if(_c=='.')_readFloatingPoint(x,double);if(_sign)(x)=-(x);}while(0)
#define rld(x) do{_readSignAndNum(x);if(_c=='.')_readFloatingPoint(x,ld);if(_sign)(x)=-(x);}while(0)
#define rcs(x) do{_cur=0;do{_c=_getchar();}while(_c<=' ');do{(x)[_cur++]=_c;}while((_c=_getchar())>' ');(x)[_cur]='\0';}while(0)
#define rs(x) do{if(!_tempInputBuf)assert(0);rcs(_tempInputBuf);(x)=string(_tempInputBuf,_cur);}while(0)
#define rcln(x) do{_cur=0;do{_c=_getchar();}while(_c<=' ');do{(x)[_cur++]=_c;}while((_c=_getchar())>=' ');(x)[_cur]='\0';}while(0)
#define rln(x) do{if(!_tempInputBuf)assert(0);rcln(_tempInputBuf);(x)=string(_tempInputBuf,_cur);}while(0)
#define setLength(x) do{if(_tempInputBuf)delete[](_tempInputBuf);_tempInputBuf=new char[(x)+1];}while(0)
void read(int&x){ri(x);}void read(uint&x){ri(x);}void read(ll&x){ri(x);}void read(ull&x){ri(x);}void read(double&x){rd(x);}void read(ld&x){rld(x);}
void read(char&x){rc(x);}void read(char*x){rcs(x);}void read(string&x){rs(x);}bool hasNext(){while(_hasNext()&&_peekchar()<=' ')_getchar();return _hasNext();}
template<typename T,typename...Ts>void read(T&&x,Ts&&...xs){read(x);read(forward<Ts>(xs)...);}
#define _flush() do{_flushBuf();fflush(stdout);}while(0)
#define _flushBuf() (fwrite(_outputBuffer,1,_outputPtr,stdout),_outputPtr=0)
#define _putchar(x) (_outputBuffer[_outputPtr==_bufferSize?_flushBuf():_outputPtr]=(x),_outputPtr++)
#define _writeTempBuf(x) (_tempOutputBuf[_tempOutputPtr++]=(x))
#define _writeOutput() for(int _i=0;_i<_tempOutputPtr;_putchar(_tempOutputBuf[_i++]));_tempOutputPtr=0
#define _writeNum(x,T,digits) _cnt=0;for(T _y=(x);_y;_y/=10,_cnt++)_numBuf[_numPtr++]='0'+_y%10;for(;_cnt<digits;_cnt++)_numBuf[_numPtr++]='0';_flushNumBuf();
#define _writeFloatingPoint(x,T) ull _I=(ull)(x);ull _F=((x)-_I)*_precisionBase+(T)(0.5);if(_F>=_precisionBase){_I++;_F=0;}_writeNum(_I,ull,1);_writeTempBuf('.');_writeNum(_F,ull,_precision)
#define _checkFinite(x) if(std::isnan(x)){wcs("NaN");}else if(std::isinf(x)){wcs("Inf");}
#define _flushNumBuf() for(;_numPtr;_writeTempBuf(_numBuf[--_numPtr]))
#define _fillBuf(x) for(int _i=0;_i<(x);_i++)_putchar(_fill)
#define _flushTempBuf() int _tempLen=_tempOutputPtr;_fillBuf(_width-_tempLen);_writeOutput();_fillBuf(-_width-_tempLen)
#define wb(x) do{if(x)_writeTempBuf('1');else _writeTempBuf('0');_flushTempBuf();}while(0)
#define wc(x) do{_writeTempBuf(x); _flushTempBuf();}while(0)
#define wi(x) do{if((x)<0){_writeTempBuf('-');_writeNum(-(x),uint,1);}else{_writeNum(x,uint,1);}_flushTempBuf();}while(0)
#define wll(x) do{if((x)<0){_writeTempBuf('-');_writeNum(-(x),ull,1);}else{_writeNum(x,ull,1);}_flushTempBuf();}while(0)
#define wd(x) do{_checkFinite(x)else if((x)<0){_writeTempBuf('-');_writeFloatingPoint(-(x),double);}else{_writeFloatingPoint(x,double);}_flushTempBuf();}while(0)
#define wld(x) do{_checkFinite(x)else if((x)<0){_writeTempBuf('-');_writeFloatingPoint(-(x),ld);}else{_writeFloatingPoint(x,ld);}_flushTempBuf();}while(0)
#define wcs(x) do{int _slen=strlen(x);_fillBuf(_width-_slen);for(const char*_p=(x);*_p;_putchar(*_p++));_fillBuf(-_width-_slen);}while(0)
#define ws(x) do{_fillBuf(_width-(int)(x).length());for(int _i=0;_i<(int)(x).length();_putchar(x[_i++]));_fillBuf(-_width-(int)(x).length());}while(0)
#define setPrecision(x) do{_precision=(x);_precisionBase=1;for(int _i=0;_i<(x);_i++,_precisionBase*=10);}while(0)
#define setDelimiter(x) do{_delimiter=(x);}while(0)
#define setWidth(x) do{_width=(x);}while(0)
#define setFill(x) do{_fill=(x);}while(0)
void write(bool x){wb(x);}void write(int x){wi(x);}void write(uint x){wi(x);}void write(ll x){wll(x);}void write(ull x){wll(x);}void write(double x){wd(x);}void write(ld x){wld(x);}
void write(char x){wc(x);}void write(char*x){wcs(x);}void write(const char*x){wcs(x);}void write(string&x){ws(x);}
template<typename T,typename...Ts>void write(T&&x,Ts&&...xs){write(x);for(const char*_p=_delimiter;*_p;_putchar(*_p++));write(forward<Ts>(xs)...);}
void writeln(){_putchar('\n');}template<typename...Ts>void writeln(Ts&&...xs){write(forward<Ts>(xs)...);_putchar('\n');}
void flush(){_flush();}class IOManager{public:~IOManager(){flush();}};unique_ptr<IOManager>iomanager;

#define MAXN 5005

int N, Q, ***BIT;

void update(int a, int x, int y, int v) {
    for (int i = x; i <= N; i += i & -i) for (int j = y; j <= (a == 0 ? N : 2 * N); j += j & -j) BIT[a][i][j] += v;
}

int rsq(int a, int x, int y) {
    int sum = 0;
    for (int i = x; i > 0; i -= i & -i) for (int j = y; j > 0; j -= j & -j) sum += BIT[a][i][j];
    return sum;
}

int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    iomanager.reset(new IOManager());
    read(N, Q);
    BIT = new int**[3];
    FOR(i, 3) {
        BIT[i] = new int*[MAXN];
        FOR(j, N + 1) {
            if (i == 0) {
                BIT[i][j] = new int[MAXN];
                FOR(k, N + 1) BIT[i][j][k] = 0;
            } else {
                BIT[i][j] = new int[MAXN * 2];
                FOR(k, 2 * N + 1) BIT[i][j][k] = 0;
            }
        }
    }
    int t, d, x, y, L;
    FOR(q, Q) {
        read(t);
        if (t == 1) {
            read(d, x, y, L);
            if (L == 0) {
                update(0, x, y, 1);
                update(0, x + 1, y, -1);
                update(0, x, y + 1, -1);
                update(0, x + 1, y + 1, 1);
            } else if (d == 1) {
                update(0, x, y, 1);
                update(0, x + L + 1, y, -1);
                update(1, x, x + y + L + 1, -1);
                update(1, x + L + 1, x + y + L + 1, 1);
            } else if (d == 2) {
                update(0, x, y + 1, -1);
                update(0, x + L + 1, y + 1, 1);
                update(2, x, N - x + y - L, 1);
                update(2, x + L + 1, N - x + y - L, -1);
            } else if (d == 3) {
                update(0, x - L, y, 1);
                update(0, x + 1, y, -1);
                update(2, x - L, N - x + y + L + 1, -1);
                update(2, x + 1, N - x + y + L + 1, 1);
            } else {
                update(0, x - L, y + 1, -1);
                update(0, x + 1, y + 1, 1);
                update(1, x - L, x + y - L + 1, 1);
                update(1, x + 1, x + y - L + 1, -1);
            }
        } else {
            read(x, y);
            writeln(rsq(0, x, y) + rsq(1, x, x + y) + rsq(2, x, N - x + y));
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define D_INF numeric_limits<double>::infinity()
#define MIN(a, b) ((a) = min((a), (b)))
#define MAX(a, b) ((a) = max((a), (b)))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define all(a) (a).begin(), (a).end()
#define For(i, a, b) for (auto i = (a); i < (b); i++)
#define FOR(i, b) For(i, 0, b)
#define Rev(i, a, b) for (auto i = (a); i > (b); i--)
#define REV(i, a) Rev(i, a, -1)
#define sz(a) ((int) (a).size())
#define nl '\n'
#define sp ' '
#define ll long long
#define ld long double
#define uint unsigned int
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pill pair<int, ll>
#define plli pair<ll, int>
#define pdd pair<double, double>
#define uset unordered_set
#define umap unordered_map
#define pq priority_queue
template<typename T> using minpq = pq<T, vector<T>, greater<T>>;
template<typename T> using maxpq = pq<T, vector<T>, less<T>>;
template<typename T1,typename T2,typename H1=hash<T1>,typename H2=hash<T2>>struct pair_hash{size_t operator()(const pair<T1,T2>&p)const{return 31*H1{}(p.first)+H2{}(p.second);}};

#define _bufferSize 4096
#define _maxNumLength 128
char _inputBuffer[_bufferSize+1],*_inputPtr=_inputBuffer,_outputBuffer[_bufferSize],_c,_sign,*_tempInputBuf=nullptr,_numBuf[_maxNumLength],_tempOutputBuf[_maxNumLength],_fill=' ';
const char*_delimiter=" ";int _cur,_outputPtr=0,_numPtr=0,_precision=6,_width=0,_tempOutputPtr=0,_cnt;ull _precisionBase=1000000;
#define _peekchar() (*_inputPtr?*_inputPtr:(_inputBuffer[fread(_inputPtr=_inputBuffer,1,_bufferSize,stdin)]='\0',*_inputPtr))
#define _getchar() (*_inputPtr?*_inputPtr++:(_inputBuffer[fread(_inputPtr=_inputBuffer,1,_bufferSize,stdin)]='\0',*_inputPtr++))
#define _hasNext() (*_inputPtr||!feof(stdin))
#define _readSignAndNum(x) do{(x)=_getchar();}while((x)<=' ');_sign=(x)=='-';if(_sign)(x)=_getchar();for((x)-='0';(_c=_getchar())>='0';(x)=(x)*10+_c-'0')
#define _readFloatingPoint(x,T) for(T _div=1.0;(_c=_getchar())>='0';(x)+=(_c-'0')/(_div*=10))
#define rc(x) do{do{(x)=_getchar();}while((x)<=' ');}while(0)
#define ri(x) do{_readSignAndNum(x);if(_sign)(x)=-(x);}while(0)
#define rd(x) do{_readSignAndNum(x);if(_c=='.')_readFloatingPoint(x,double);if(_sign)(x)=-(x);}while(0)
#define rld(x) do{_readSignAndNum(x);if(_c=='.')_readFloatingPoint(x,ld);if(_sign)(x)=-(x);}while(0)
#define rcs(x) do{_cur=0;do{_c=_getchar();}while(_c<=' ');do{(x)[_cur++]=_c;}while((_c=_getchar())>' ');(x)[_cur]='\0';}while(0)
#define rs(x) do{if(!_tempInputBuf)assert(0);rcs(_tempInputBuf);(x)=string(_tempInputBuf,_cur);}while(0)
#define rcln(x) do{_cur=0;do{_c=_getchar();}while(_c<=' ');do{(x)[_cur++]=_c;}while((_c=_getchar())>=' ');(x)[_cur]='\0';}while(0)
#define rln(x) do{if(!_tempInputBuf)assert(0);rcln(_tempInputBuf);(x)=string(_tempInputBuf,_cur);}while(0)
#define setLength(x) do{if(_tempInputBuf)delete[](_tempInputBuf);_tempInputBuf=new char[(x)+1];}while(0)
void read(int&x){ri(x);}void read(uint&x){ri(x);}void read(ll&x){ri(x);}void read(ull&x){ri(x);}void read(double&x){rd(x);}void read(ld&x){rld(x);}
void read(char&x){rc(x);}void read(char*x){rcs(x);}void read(string&x){rs(x);}bool hasNext(){while(_hasNext()&&_peekchar()<=' ')_getchar();return _hasNext();}
template<typename T,typename...Ts>void read(T&&x,Ts&&...xs){read(x);read(forward<Ts>(xs)...);}
#define _flush() do{_flushBuf();fflush(stdout);}while(0)
#define _flushBuf() (fwrite(_outputBuffer,1,_outputPtr,stdout),_outputPtr=0)
#define _putchar(x) (_outputBuffer[_outputPtr==_bufferSize?_flushBuf():_outputPtr]=(x),_outputPtr++)
#define _writeTempBuf(x) (_tempOutputBuf[_tempOutputPtr++]=(x))
#define _writeOutput() for(int _i=0;_i<_tempOutputPtr;_putchar(_tempOutputBuf[_i++]));_tempOutputPtr=0
#define _writeNum(x,T,digits) _cnt=0;for(T _y=(x);_y;_y/=10,_cnt++)_numBuf[_numPtr++]='0'+_y%10;for(;_cnt<digits;_cnt++)_numBuf[_numPtr++]='0';_flushNumBuf();
#define _writeFloatingPoint(x,T) ull _I=(ull)(x);ull _F=((x)-_I)*_precisionBase+(T)(0.5);if(_F>=_precisionBase){_I++;_F=0;}_writeNum(_I,ull,1);_writeTempBuf('.');_writeNum(_F,ull,_precision)
#define _checkFinite(x) if(std::isnan(x)){wcs("NaN");}else if(std::isinf(x)){wcs("Inf");}
#define _flushNumBuf() for(;_numPtr;_writeTempBuf(_numBuf[--_numPtr]))
#define _fillBuf(x) for(int _i=0;_i<(x);_i++)_putchar(_fill)
#define _flushTempBuf() int _tempLen=_tempOutputPtr;_fillBuf(_width-_tempLen);_writeOutput();_fillBuf(-_width-_tempLen)
#define wb(x) do{if(x)_writeTempBuf('1');else _writeTempBuf('0');_flushTempBuf();}while(0)
#define wc(x) do{_writeTempBuf(x); _flushTempBuf();}while(0)
#define wi(x) do{if((x)<0){_writeTempBuf('-');_writeNum(-(x),uint,1);}else{_writeNum(x,uint,1);}_flushTempBuf();}while(0)
#define wll(x) do{if((x)<0){_writeTempBuf('-');_writeNum(-(x),ull,1);}else{_writeNum(x,ull,1);}_flushTempBuf();}while(0)
#define wd(x) do{_checkFinite(x)else if((x)<0){_writeTempBuf('-');_writeFloatingPoint(-(x),double);}else{_writeFloatingPoint(x,double);}_flushTempBuf();}while(0)
#define wld(x) do{_checkFinite(x)else if((x)<0){_writeTempBuf('-');_writeFloatingPoint(-(x),ld);}else{_writeFloatingPoint(x,ld);}_flushTempBuf();}while(0)
#define wcs(x) do{int _slen=strlen(x);_fillBuf(_width-_slen);for(const char*_p=(x);*_p;_putchar(*_p++));_fillBuf(-_width-_slen);}while(0)
#define ws(x) do{_fillBuf(_width-(int)(x).length());for(int _i=0;_i<(int)(x).length();_putchar(x[_i++]));_fillBuf(-_width-(int)(x).length());}while(0)
#define setPrecision(x) do{_precision=(x);_precisionBase=1;for(int _i=0;_i<(x);_i++,_precisionBase*=10);}while(0)
#define setDelimiter(x) do{_delimiter=(x);}while(0)
#define setWidth(x) do{_width=(x);}while(0)
#define setFill(x) do{_fill=(x);}while(0)
void write(bool x){wb(x);}void write(int x){wi(x);}void write(uint x){wi(x);}void write(ll x){wll(x);}void write(ull x){wll(x);}void write(double x){wd(x);}void write(ld x){wld(x);}
void write(char x){wc(x);}void write(char*x){wcs(x);}void write(const char*x){wcs(x);}void write(string&x){ws(x);}
template<typename T,typename...Ts>void write(T&&x,Ts&&...xs){write(x);for(const char*_p=_delimiter;*_p;_putchar(*_p++));write(forward<Ts>(xs)...);}
void writeln(){_putchar('\n');}template<typename...Ts>void writeln(Ts&&...xs){write(forward<Ts>(xs)...);_putchar('\n');}
void flush(){_flush();}class IOManager{public:~IOManager(){flush();}};unique_ptr<IOManager>iomanager;

#define MAXN 5005

int N, Q, ***BIT;

void update(int a, int x, int y, int v) {
    for (int i = x; i <= N; i += i & -i) for (int j = y; j <= (a == 0 ? N : 2 * N); j += j & -j) BIT[a][i][j] += v;
}

int rsq(int a, int x, int y) {
    int sum = 0;
    for (int i = x; i > 0; i -= i & -i) for (int j = y; j > 0; j -= j & -j) sum += BIT[a][i][j];
    return sum;
}

int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    iomanager.reset(new IOManager());
    read(N, Q);
    BIT = new int**[3];
    FOR(i, 3) {
        BIT[i] = new int*[MAXN];
        FOR(j, N + 1) {
            if (i == 0) {
                BIT[i][j] = new int[MAXN];
                FOR(k, N + 1) BIT[i][j][k] = 0;
            } else {
                BIT[i][j] = new int[MAXN * 2];
                FOR(k, 2 * N + 1) BIT[i][j][k] = 0;
            }
        }
    }
    int t, d, x, y, L;
    FOR(q, Q) {
        read(t);
        if (t == 1) {
            read(d, x, y, L);
            if (L == 0) {
                update(0, x, y, 1);
                update(0, x + 1, y, -1);
                update(0, x, y + 1, -1);
                update(0, x + 1, y + 1, 1);
            } else if (d == 1) {
                update(0, x, y, 1);
                update(0, x + L + 1, y, -1);
                update(1, x, x + y + L + 1, -1);
                update(1, x + L + 1, x + y + L + 1, 1);
            } else if (d == 2) {
                update(0, x, y + 1, -1);
                update(0, x + L + 1, y + 1, 1);
                update(2, x, N + 1 - x + y - L, 1);
                update(2, x + L + 1, N + 1 - x + y - L, -1);
            } else if (d == 3) {
                update(0, x - L, y, 1);
                update(0, x + 1, y, -1);
                update(2, x - L, N + 1 - x + y + L + 1, -1);
                update(2, x + 1, N + 1 - x + y + L + 1, 1);
            } else {
                update(0, x - L, y + 1, -1);
                update(0, x + 1, y + 1, 1);
                update(1, x - L, x + y - L + 1, 1);
                update(1, x + 1, x + y - L + 1, -1);
            }
        } else {
            read(x, y);
            writeln(rsq(0, x, y) + rsq(1, x, x + y) + rsq(2, x, N + 1 - x + y));
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define D_INF numeric_limits<double>::infinity()
#define MIN(a, b) ((a) = min((a), (b)))
#define MAX(a, b) ((a) = max((a), (b)))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define all(a) (a).begin(), (a).end()
#define For(i, a, b) for (auto i = (a); i < (b); i++)
#define FOR(i, b) For(i, 0, b)
#define Rev(i, a, b) for (auto i = (a); i > (b); i--)
#define REV(i, a) Rev(i, a, -1)
#define sz(a) ((int) (a).size())
#define nl '\n'
#define sp ' '
#define ll long long
#define ld long double
#define uint unsigned int
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pill pair<int, ll>
#define plli pair<ll, int>
#define pdd pair<double, double>
#define uset unordered_set
#define umap unordered_map
#define pq priority_queue
template<typename T> using minpq = pq<T, vector<T>, greater<T>>;
template<typename T> using maxpq = pq<T, vector<T>, less<T>>;
template<typename T1,typename T2,typename H1=hash<T1>,typename H2=hash<T2>>struct pair_hash{size_t operator()(const pair<T1,T2>&p)const{return 31*H1{}(p.first)+H2{}(p.second);}};

#define _bufferSize 4096
#define _maxNumLength 128
char _inputBuffer[_bufferSize+1],*_inputPtr=_inputBuffer,_outputBuffer[_bufferSize],_c,_sign,*_tempInputBuf=nullptr,_numBuf[_maxNumLength],_tempOutputBuf[_maxNumLength],_fill=' ';
const char*_delimiter=" ";int _cur,_outputPtr=0,_numPtr=0,_precision=6,_width=0,_tempOutputPtr=0,_cnt;ull _precisionBase=1000000;
#define _peekchar() (*_inputPtr?*_inputPtr:(_inputBuffer[fread(_inputPtr=_inputBuffer,1,_bufferSize,stdin)]='\0',*_inputPtr))
#define _getchar() (*_inputPtr?*_inputPtr++:(_inputBuffer[fread(_inputPtr=_inputBuffer,1,_bufferSize,stdin)]='\0',*_inputPtr++))
#define _hasNext() (*_inputPtr||!feof(stdin))
#define _readSignAndNum(x) do{(x)=_getchar();}while((x)<=' ');_sign=(x)=='-';if(_sign)(x)=_getchar();for((x)-='0';(_c=_getchar())>='0';(x)=(x)*10+_c-'0')
#define _readFloatingPoint(x,T) for(T _div=1.0;(_c=_getchar())>='0';(x)+=(_c-'0')/(_div*=10))
#define rc(x) do{do{(x)=_getchar();}while((x)<=' ');}while(0)
#define ri(x) do{_readSignAndNum(x);if(_sign)(x)=-(x);}while(0)
#define rd(x) do{_readSignAndNum(x);if(_c=='.')_readFloatingPoint(x,double);if(_sign)(x)=-(x);}while(0)
#define rld(x) do{_readSignAndNum(x);if(_c=='.')_readFloatingPoint(x,ld);if(_sign)(x)=-(x);}while(0)
#define rcs(x) do{_cur=0;do{_c=_getchar();}while(_c<=' ');do{(x)[_cur++]=_c;}while((_c=_getchar())>' ');(x)[_cur]='\0';}while(0)
#define rs(x) do{if(!_tempInputBuf)assert(0);rcs(_tempInputBuf);(x)=string(_tempInputBuf,_cur);}while(0)
#define rcln(x) do{_cur=0;do{_c=_getchar();}while(_c<=' ');do{(x)[_cur++]=_c;}while((_c=_getchar())>=' ');(x)[_cur]='\0';}while(0)
#define rln(x) do{if(!_tempInputBuf)assert(0);rcln(_tempInputBuf);(x)=string(_tempInputBuf,_cur);}while(0)
#define setLength(x) do{if(_tempInputBuf)delete[](_tempInputBuf);_tempInputBuf=new char[(x)+1];}while(0)
void read(int&x){ri(x);}void read(uint&x){ri(x);}void read(ll&x){ri(x);}void read(ull&x){ri(x);}void read(double&x){rd(x);}void read(ld&x){rld(x);}
void read(char&x){rc(x);}void read(char*x){rcs(x);}void read(string&x){rs(x);}bool hasNext(){while(_hasNext()&&_peekchar()<=' ')_getchar();return _hasNext();}
template<typename T,typename...Ts>void read(T&&x,Ts&&...xs){read(x);read(forward<Ts>(xs)...);}
#define _flush() do{_flushBuf();fflush(stdout);}while(0)
#define _flushBuf() (fwrite(_outputBuffer,1,_outputPtr,stdout),_outputPtr=0)
#define _putchar(x) (_outputBuffer[_outputPtr==_bufferSize?_flushBuf():_outputPtr]=(x),_outputPtr++)
#define _writeTempBuf(x) (_tempOutputBuf[_tempOutputPtr++]=(x))
#define _writeOutput() for(int _i=0;_i<_tempOutputPtr;_putchar(_tempOutputBuf[_i++]));_tempOutputPtr=0
#define _writeNum(x,T,digits) _cnt=0;for(T _y=(x);_y;_y/=10,_cnt++)_numBuf[_numPtr++]='0'+_y%10;for(;_cnt<digits;_cnt++)_numBuf[_numPtr++]='0';_flushNumBuf();
#define _writeFloatingPoint(x,T) ull _I=(ull)(x);ull _F=((x)-_I)*_precisionBase+(T)(0.5);if(_F>=_precisionBase){_I++;_F=0;}_writeNum(_I,ull,1);_writeTempBuf('.');_writeNum(_F,ull,_precision)
#define _checkFinite(x) if(std::isnan(x)){wcs("NaN");}else if(std::isinf(x)){wcs("Inf");}
#define _flushNumBuf() for(;_numPtr;_writeTempBuf(_numBuf[--_numPtr]))
#define _fillBuf(x) for(int _i=0;_i<(x);_i++)_putchar(_fill)
#define _flushTempBuf() int _tempLen=_tempOutputPtr;_fillBuf(_width-_tempLen);_writeOutput();_fillBuf(-_width-_tempLen)
#define wb(x) do{if(x)_writeTempBuf('1');else _writeTempBuf('0');_flushTempBuf();}while(0)
#define wc(x) do{_writeTempBuf(x); _flushTempBuf();}while(0)
#define wi(x) do{if((x)<0){_writeTempBuf('-');_writeNum(-(x),uint,1);}else{_writeNum(x,uint,1);}_flushTempBuf();}while(0)
#define wll(x) do{if((x)<0){_writeTempBuf('-');_writeNum(-(x),ull,1);}else{_writeNum(x,ull,1);}_flushTempBuf();}while(0)
#define wd(x) do{_checkFinite(x)else if((x)<0){_writeTempBuf('-');_writeFloatingPoint(-(x),double);}else{_writeFloatingPoint(x,double);}_flushTempBuf();}while(0)
#define wld(x) do{_checkFinite(x)else if((x)<0){_writeTempBuf('-');_writeFloatingPoint(-(x),ld);}else{_writeFloatingPoint(x,ld);}_flushTempBuf();}while(0)
#define wcs(x) do{int _slen=strlen(x);_fillBuf(_width-_slen);for(const char*_p=(x);*_p;_putchar(*_p++));_fillBuf(-_width-_slen);}while(0)
#define ws(x) do{_fillBuf(_width-(int)(x).length());for(int _i=0;_i<(int)(x).length();_putchar(x[_i++]));_fillBuf(-_width-(int)(x).length());}while(0)
#define setPrecision(x) do{_precision=(x);_precisionBase=1;for(int _i=0;_i<(x);_i++,_precisionBase*=10);}while(0)
#define setDelimiter(x) do{_delimiter=(x);}while(0)
#define setWidth(x) do{_width=(x);}while(0)
#define setFill(x) do{_fill=(x);}while(0)
void write(bool x){wb(x);}void write(int x){wi(x);}void write(uint x){wi(x);}void write(ll x){wll(x);}void write(ull x){wll(x);}void write(double x){wd(x);}void write(ld x){wld(x);}
void write(char x){wc(x);}void write(char*x){wcs(x);}void write(const char*x){wcs(x);}void write(string&x){ws(x);}
template<typename T,typename...Ts>void write(T&&x,Ts&&...xs){write(x);for(const char*_p=_delimiter;*_p;_putchar(*_p++));write(forward<Ts>(xs)...);}
void writeln(){_putchar('\n');}template<typename...Ts>void writeln(Ts&&...xs){write(forward<Ts>(xs)...);_putchar('\n');}
void flush(){_flush();}class IOManager{public:~IOManager(){flush();}};unique_ptr<IOManager>iomanager;

#define MAXN 5005

int N, Q, BIT[3][MAXN][MAXN * 2];

void update(int a, int x, int y, int v) {
    for (int i = x; i <= N; i += i & -i) for (int j = y; j <= 2 * N; j += j & -j) BIT[a][i][j] += v;
}

int rsq(int a, int x, int y) {
    int sum = 0;
    for (int i = x; i > 0; i -= i & -i) for (int j = y; j > 0; j -= j & -j) sum += BIT[a][i][j];
    return sum;
}

int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    iomanager.reset(new IOManager());
    read(N, Q);
    FOR(i, 3) FOR(j, N + 1) FOR(k, 2 * N + 1) BIT[i][j][k] = 0;
    int t, d, x, y, L;
    FOR(q, Q) {
        read(t);
        if (t == 1) {
            read(d, x, y, L);
            if (L == 0) {
                update(0, x, y, 1);
                update(0, x + 1, y, -1);
                update(0, x, y + 1, -1);
                update(0, x + 1, y + 1, 1);
            } else if (d == 1) {
                update(0, x, y, 1);
                update(0, x + L + 1, y, -1);
                update(1, x, x + y + L + 1, -1);
                update(1, x + L + 1, x + y + L + 1, 1);
            } else if (d == 2) {
                update(0, x, y + 1, -1);
                update(0, x + L + 1, y + 1, 1);
                update(2, x, N - x + y - L, 1);
                update(2, x + L + 1, N - x + y - L, -1);
            } else if (d == 3) {
                update(0, x - L, y, 1);
                update(0, x + 1, y, -1);
                update(2, x - L, N - x + y + L + 1, -1);
                update(2, x + 1, N - x + y + L + 1, 1);
            } else {
                update(0, x - L, y + 1, -1);
                update(0, x + 1, y + 1, 1);
                update(1, x - L + 1, x + y - L + 1, 1);
                update(1, x + 1, x + y - L + 1, -1);
            }
        } else {
            read(x, y);
            writeln(rsq(0, x, y) + rsq(1, x, x + y) + rsq(2, x, N - x + y));
        }
    }
    return 0;
}