//QwQcOrZ yyds!!!
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define R(i,a,b) for (int i=(a);i<(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define go(i,x) for (int i=head[x];i;i=e[i].nx)
#define mp make_pair
#define pb push_back
#define pa pair < int,int >
#define fi first
#define se second
#define re register
#define be begin()
#define en end()
#define sqr(x) ((x)*(x))
#define ret return puts("-1"),0;
#define put putchar('\n')
#define inf 1000000005
#define mod 998244353
#define int ll
#define N 100005
using namespace std;
inline char gc(){static char buf[100000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}
#define gc getchar
inline ll read(){char c=gc();ll su=0,f=1;for (;c<'0'||c>'9';c=gc()) if (c=='-') f=-1;for (;c>='0'&&c<='9';c=gc()) su=su*10+c-'0';return su*f;}
inline void write(ll x){if (x<0){putchar('-');write(-x);return;}if (x>=10) write(x/10);putchar(x%10+'0');}
inline void writesp(ll x){write(x),putchar(' ');}
inline void writeln(ll x){write(x);putchar('\n');}
int n,t[N],l[N],r[N],x,y,ans;
signed main()
{
	n=read();
	for (int i=1;i<=n;i++)
	{
		t[i]=read(),l[i]=read(),r[i]=read();
	}
	for (int i=1;i<=n;i++)
		for (int j=i+1;j<=n;j++)
		{
			x=i,y=j;
			if (l[x]>l[y]) swap(x,y);
			if (l[y]<r[x]) ans++;else
			if (l[y]==r[x])
			{
				if ((t[x]==1||t[x]==3)&&(t[y]==2||t[y]==1)) ans++;
			}
		}
	writeln(ans);
			
}
/*

*/


// Problem: 
// 			F - Close Group
// 			
// 		
// Contest: AtCoder - AtCoder Beginner Contest 187
// URL: https://atcoder.jp/contests/abc187/tasks/abc187_f
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 18;
int connect[N],f[1 << N];

int main()
{
	int n,m;scanf("%d%d",&n,&m);
	for(int i = 0;i < n;++i)	connect[i] |= (1 << i);
	
	for(int i = 0;i < m;++i)
	{
		int u,v;scanf("%d%d",&u,&v);
		--u;--v;
		connect[u] |= (1 << v);
		connect[v] |= (1 << u);
	}
	
	f[0] = 0;
	for(int i = 1;i < (1 << n);++i)
	{
		f[i] = 1e5+7;
		bool indp = 1;
		for(int j = 0;j < n;++j)
			if((i >> j & 1) && (connect[j] & i) != i)
				indp = 0;
		if(indp)	f[i] = 1;
		for(int s = (i - 1) & i;s;s = (s - 1) & i)
			f[i] = min(f[i],f[s] + f[i ^ s]);
	}
	printf("%d",f[(1 << n) - 1]);
    return 0;
}
