///*    ***Bismillahir Rahmanir Rahim***   */
/// bujsilam ar proti index value gcd ber korte hbe
///pore je oi gcd mod oita mathaia sen ni
#include<bits/stdc++.h>
using namespace std;
typedef		long long       ll;
typedef         long double      dl;
typedef     unsigned long long  ull;
#define		pb              push_back
#define		PB              pop_back
#define		nn              "\n"
#define     O_O             ios_base::sync_with_stdio(false); cin.tie(NULL)
#define		all(p)          p.begin(),p.end()
#define		zz(v)           (ll)v.size()
#define		ss              ' '
#define	       MEM(a,b)         memset(a,(b),sizeof(a))
#define	       CLR(p)           memset(p,0,sizeof(p))
#define precision(a) fixed << setprecision(a)
#define rep(i,b) for(ll i=0;i<(b);i++)
#define rep1(i,b) for(int i=1;i<=(b);i++)
//#define rep(s,e)         for(i=s;i<e;i+=1)
//#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define fr(i,b,a) for(int i=(b);i>=(a);i--)
#define rep2(i,a,b,c) for(int i=(a);i!=(b);i+=(c))
#define         arrsize(a)      (sizeof(a)/sizeof(a[0]))
//#define       arrsize(a)      (sizeof(a)/sizeof(*a))
#define 	S(a)            scanf("%lld",&a)
#define 	SS(a,b)         scanf("%lld %lld",&a,&b)
#define 	SSS(a,b,c)      scanf("%lld %lld %lld",&a,&b,&c)
#define		gcd(a,b)        __gcd(a,b)
#define		lcm(a,b)        (a*b)/gcd(a,b)
#define		pi              acos(-1.0)
#define	       ff               first
#define	       sc              second
#define     print(v)        for(ll i:v) cout<<i<<ss
typedef        pair <int, int>   pii;
typedef       pair <ll, ll>      pll;
typedef      vector< pair <ll, ll> >      vpll;
typedef		vector<ll>      vll;
typedef		map<string,ll>      msl;
typedef		map<ll,ll>      mll;
#define yes cout << "YES\n"
#define no cout<<"NO\n"
bool comp(pair<ll,ll> a,pair<ll,ll> b)
{
    if(a.first != b.first) return a.first > b.first;
    else
    return a.second > b.second;
}
//ll minelementindex = min_element(v.begin(),v.end()) - v.begin();
//ll minelement = *min_element(v.begin(), v.end());
//ll maxelementindex = max_element(v.begin(),v.end()) - v.begin();
//ll maxelement = *max_element(v.begin(), v.end());
//memset(ar,-1,sizeof(ar));
//#define sort(x) sort(x.begin(), x.end())
//sort(a,a+n,greater<ll>())
//for (auto it = mp.begin(); it != mp.end(); ++it){}
// string x(w.size(),'1');
#define MAX 1000000
#define precision(a) fixed << setprecision(a)

#define mod 1000000007
 ll cheak(ll x)
 {

    string s =to_string(x);
     string x1;
     x1=s;
     s+=s;
      stringstream geek(s);

    // The object has the value 12345 and stream
    // it to the integer x
    ll y = 0;
    geek >> y;
   // ll y = stoi(s);
     return y;
 }



int main()
{
 //O_O ;
   ll t=1;
   //cin>>t;
   while(t--){
       ll n;cin>>n;
       ll c=0;
   for(ll i=1;i<=1000000;i++)
   {
       ll x2=cheak(i);
       if(x2<=n){
        c++;
      //  cout<<x2<<nn;
       }

   }
   cout<<c<<nn;






   }


    return 0;
}








//https://codeforces.com/contest/1371/problem/D korte hbe
//https://codeforces.com/contest/1256/problem/B implementation pari nai
/*****************  ALHAMDULILLAH  *****************/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i,n) for(ll i=0; i<ll(n); i++)
#define FOR(i,m,n) for(ll i=ll(m); i<ll(n); i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define VI vector<int>
#define VP vector<pair<ll,ll>>
#define VPP vector<pair<int,pair<int,int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i,a) for(auto &i:a)
typedef pair <int, int> P;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }

const int  INF = (1 << 30) - 1;
const ll INFL = 1LL << 61;
//const ll mod = 1e9 + 7;
const ll mod = 998244353;



int main() {
	ll n, m;
	cin >> n >> m;
	VI x(m), y(m), z(m);
	REP(i, m) {
		cin >> x[i] >> y[i] >> z[i];
	}
	VLL dp((1 << n) + 1, 0);
	dp[0] = 1;

	REP(i, (1 << n)) {
		if (dp[i] == 0)continue;
		VI v;
		VI cand;
		REP(j, n) {
			if (i&(1 << j))v.push_back(j);
			else cand.push_back(j);
		}
		int v_size = v.size();

		for (int j : cand) {
			bool flag = true;
			REP(k, m) {
				if (x[k] <= v_size)continue;
				if (j >= y[k])continue;
				int cnt = upper_bound(ALL(v), y[k]-1) - v.begin();
				if (cnt == z[k])flag = false;
			}
			if (flag) {
				dp[(i ^ (1 << j))] += dp[i];
			}
		}
	}
	cout << dp[(1 << n)-1] << endl;

}
