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

const int maxn = 1 << 19;

int n, m;
long long dp[maxn]; bool ok[maxn];
int x[110], y[110], z[110];

int main()
{
    scanf ("%d %d", &n, &m);
    for (int i = 1; i <= m; i++)
        scanf ("%d %d %d", x + i, y + i, z + i);
    int upp = 1 << n;
    dp[0] = 1;
    for (int sta = 0; sta < upp; sta++)
    {
        bool f = 1;
        int cnt = __builtin_popcount(sta);
        for (int i = 1; i <= m; i++)
        {
            if (cnt > x[i]) continue;
            int u = 1 << y[i]; u--;
            int v = u & sta;
            int c = __builtin_popcount(v);
            if (c > z[i]) f = 0;
        }
        if (!f) dp[sta] = 0;
        for (int o = 0; o < n; o++) if (!((1 << o) & sta)) dp[sta | (1 << o)] += dp[sta];
    }
    printf ("%lld\n", dp[upp - 1]);
    return 0;
}
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define debug puts("WHAP")
const int N = 101;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    vector<pair<int,int> > a;
    int ans = 0;
    for(int i = 0; i < n; ++i){
    	int t,x,y; cin >> t >> x >> y;
    	x*=2;
    	y*=2;
    	if(t == 1){
    		a.push_back(make_pair(x,y));
    	}
    	else if(t == 2){
    		a.push_back(make_pair(x,y-1));
    	}
    	else if(t == 3){
    		a.push_back(make_pair(x+1, y));
    	}
    	else{
    		a.push_back(make_pair(x+1,y-1));
    	}

    }
    for(int i = 0; i < n; ++i){
    	for(int j = i+1; j < n; ++j){
    		if(a[i].second < a[j].first){
    			continue;
    		}
    		if(a[j].second < a[i].first){
    			continue;
    		}
    		++ans;
    	}
    }
    cout << ans << "\n";

	return 0;
}