#include <bits/stdc++.h>

typedef long long int ll;
typedef long double ld;
using namespace std;
#define P 1000000007
#define rep(i,n) for(i=0;i<n;++i)
#define re(i,a,n) for(i=a;i<=n;++i)
#define repr(i,a,n) for(i=a;i>=n;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define mset(a,val) memset(a,val,sizeof(a));
#define at(s,pos) *(s.find_by_order(pos))
#define set_ind(s,val) s.order_of_key(val)
#define PI 3.14159265
#define block_size ((ll)sqrt(3e5) + 1) 
/*#include <ext/pb_ds/assoc_container.hpp> // Common file  
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional> // for less  
#include <iostream>  
// Important header files   
using namespace __gnu_pbds;  
using namespace std;  
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/

const int maxn = 1<<18 ;
ll dp[maxn];

int main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   ll t,i,j,l,r,f,e,x,y,mid,z,w,k,d,c,n,a,b,m;
   ll t1,t2,t3,ans=0,tc ;
   t=1;
   cin >> n >> m ;
   vector<pair<ll,pair<ll,ll>>> co(m);
   rep(i,m)
   {
      cin >> co[i].fi >> co[i].se.fi >> co[i].se.se ;
   }
   sortv(co);
   x=1<<n;
   
   mset(dp,0);
   dp[0]=1;

   re(i,0,x-2)
   {
      
      ll mx=0;
      //cout << i << endl ;
      f=0;
      rep(j,m)
      {
          t1=0;
          t2=__builtin_popcount(i);
          if(t2>=co[j].fi) continue;
          rep(k,n)
          {
            y=1<<k;
            if((y&i)!=0)
            {
              //cout << k << " " ;
              if(k<co[j].se.fi) t1++;
            }
          }
          if(t1==co[j].se.se) mx=max(mx,co[j].se.fi);
          if(t1>co[j].se.se) f=1;
      }

      //cout << endl ;
      //cout << i << " " ;
      //cout << mx << endl ;
      if(f) continue;
      for(j=mx;j<n;++j)
      {
        y=1<<j;
        //cout << j << endl ;
        if(y&i) continue;
        dp[i|y]+=dp[i];

      }
      
   }
   //rep(i,x) cout << dp[i] << " " ;

   cout << dp[x-1] << endl ;
   
 
    
  return 0;
        
}
#include "bits/stdc++.h"
#include<sstream>
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define NIL = -1;
#define all(x) x.begin(),x.end()
const ll INF = 1e9;
const long long inf = 1e18;
const ll INFL = 1e18;
const ll MOD = 1e9 + 7;

int digit(ll x) {
    int digits = 0;
 
    while(x > 0){
        x /= 10;
        digits++;
    }
 
    return digits;
}
 
ll gcd(long long a,long long b) {
    if (a < b) swap(a,b);
    if (b == 0) return a;
    
    return gcd(b,a%b);
}
 
bool is_prime(long long N){
    if (N == 1) return false;
    for (long long i = 2;i * i <= N;i++){
        if (N % i == 0) return false;
    }
    return true;
}
 
ll lcm(ll a,ll b){
    return ((a * b == 0)) ? 0 : (a / gcd(a,b) * b);
}
 
double DegreeToRadian(double degree){
    return degree * M_PI / 180.0;
}
 
long long modpow(long long a, long long b, long long m){
    long long ans = 1;
    while(b > 0){
        if (b % 2 == 1){
            ans *= a;
            ans %= m;
        }
        a *= a;
        a %= m;
        b /= 2;
    }
    return ans;
}

long long comb(long long x, long long y){
    int z;
    if (y == 0) return 1;
    else {
        z = modpow(x, y/2, MOD)*modpow(x, y/2, MOD)%MOD;
        if (y % 2 == 1) z = z*x%MOD;
        return z;
    }
}


vector<pair<long long, long long>> prime_fact(long long x){
    vector<pair<long long, long long>> res;
    for(int i = 2;i*i <= x;i++){
        if (x % i == 0){
            long long cnt = 0;
            while(x % i == 0){
                cnt++;
                x /= i;
            }
            res.push_back({i, cnt});
        }
    }
    if (x != 1){
        res.push_back({x, 1});
    }
    return res;
}

int64_t mod_inv(int64_t a, int64_t m){
    int64_t b = m, u = 1, v = 0;

    while(b){
        int64_t t = a/b;
        a -= t*b;
        swap(a, b);
        u -= t*v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;

    return u;
}


// {g, x, y}: ax + by = g
tuple<long long, long long, long long> extgcd(long long a, long long b){
    if (b == 0) return {a, 1, 0};
    long long g, x, y;
    tie(g, x, y) = extgcd(b, a % b);
    return {g, y, x - a/b*y};
}

int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

 
//////////////////////////////////////////////////////////////


int main(){
    int n,k;
    cin >> n >> k;
    for(int i = 0;i < k;i++){
        string tmp = to_string(n);
        sort(tmp.begin(), tmp.end());
        int mini = stoi(tmp);
        sort(tmp.begin(), tmp.end(), greater<>());
        int mx = stoi(tmp);
        n = mx - mini;
    }
    cout << n << endl;
}