#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int popcount(int a){
  a = (a&0x55555555) + ((a>>1)&0x55555555);
  a = (a&0x33333333) + ((a>>2)&0x33333333);
  a = (a&0x0f0f0f0f) + ((a>>4)&0x0f0f0f0f);
  a = (a&0x00ff00ff) + ((a>>8)&0x00ff00ff);
  return (a & 0xffff) + (a>>16);
}

int N,M;
vector<vector<pair<int,int>>> Resteictions;
vector<ull> dp;

int main(){
  scanf("%d%d",&N,&M);
  Resteictions.resize(N+1);
  rep(i,M){
    int x,y,z; scanf("%d%d%d",&x,&y,&z);
    Resteictions[y].push_back({(1<<x)-1,z});
  }
  dp.assign(1<<N,0);
  dp[0] = 1;
  rep(j,1<<N){
    int c = popcount(j);
    for(auto q : Resteictions[c]){
      if(popcount(j & q.first) > q.second) dp[j] = 0;
    }
    rep(k,N){
      if((j&(1<<k)) != 0) continue;
      dp[j|(1<<k)] += dp[j];
    }
  }
  printf("%llu\n",dp[(1<<N)-1]);
  return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define eb emplace_back
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef long long int ll;
typedef pair<int, int> P;
// typedef modint1000000007 mint;

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

void speedUpIO() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
}
/*--------------------------------------------------*/

void solve() {
    int n, m;
    cin >> n >> m;
    V<int> a(n);
    rep(i, n) cin >> a[i];
    V<int> cnt(n + 1);
    rep(i, m) cnt[a[i]]++;
    int ans = n;
    rep(i, n) {
        if (cnt[i] == 0) {
            ans = i;
            break;
        }
    }
    for (int i = 1; i <= n - m; i++) {
        int l = i - 1;
        int r = i + m - 1;
        // if (cnt[a[l]] == 0) chmin(ans, a[l]);
        cnt[a[l]]--;
        cnt[a[r]]++;
        if (cnt[a[l]] == 0) chmin(ans, a[l]);
    }
    rep(i, n) {
        if (cnt[i] == 0) {
            chmin(ans, i);
            break;
        }
    }
    cout << ans << "\n";
}

int main() {
    speedUpIO();
    solve();
    return 0;
}
