#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>

#define FOR_LT(i, beg, end) for (decltype(end) i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (decltype(end) i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (decltype(beg) i = beg; end <= i; i--)
#define REP(n)              for (decltype(n) repeat_index = 0; repeat_index < n; repeat_index++)

using namespace std;

template<typename T, size_t S>
T minval(const T(&vals)[S])
{
	T val = numeric_limits<T>::max();
	FOR_LT(i, 0, S) {
		val = min(val, vals[i]);
	}
	return val;
}

template<typename T, size_t S>
T maxval(const T(&vals)[S])
{
	T val = numeric_limits<T>::min();
	FOR_LT(i, 0, S) {
		val = max(val, vals[i]);
	}
	return val;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	int n, m; cin >> n >> m;
	vector<int> as(n); for (auto& a : as) { cin >> a; }
	vector<int> cs(n + 1);
	set<int> uu;
	FOR_LE(i, 0, n + 1) uu.insert(i);

	FOR_LT(i, 0, m) {
		if (cs[as[i]] == 0) uu.erase(as[i]);
		cs[as[i]]++;
	}

	int l = 0;
	int r = m;

	int ans = *uu.begin();
	while (r != n) {
		cs[as[l]]--;
		if (cs[as[l]] == 0) uu.insert(as[l]);
		if (cs[as[r]] == 0) uu.erase(as[r]);
		cs[as[r]]++;
		ans = min(ans, *uu.begin());
		l++;
		r++;
	}
	cout << ans << endl;

	return 0;
}
#include <bits/stdc++.h>

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vvi vector<vector<int>>
#define vvii vector<vector<pair<int, int>>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\n'

constexpr int INF = (sizeof(int) == 4 ? 1e9 : 2e18) + 1e5;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

vvi adj(MAXN);
vi arr(MAXN);
bool check(int u, int c) {
  for (int v : adj[u])
    if (arr[v] == c)
      return false;
  return true;
}

vi dd(MAXN);
set<int> s;
int n;
int cnt3 = 0;
int brute(int idx) {
  if (idx == n)
    return 1;

  if (dd[idx] == 0) {
    s.emplace(idx);
    return brute(idx + 1);
  }

  int ans = 0;
  for (int i = 0; i < 3; ++i)
    if (check(idx, i)) {
      arr[idx] = i;
      ans += brute(idx + 1);
      arr[idx] = -1;
    }
  return ans;
}

int poww() {
  int ans = 1;
  for (int i = 0; i < s.size(); ++i)
    ans *= 3;
  return ans;
}

// #define MULTIPLE_TEST_CASES
void solve(const int test) {
  cin >> n;
  int m;
  cin >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    if (u < v)
      swap(u, v);
    ++dd[u];
    ++dd[v];
    adj[u].eb(v);
  }
  int val = brute(0);
  cout << poww() * val << endl;
}

signed main() {
  // const string FILE_NAME = "";
  // freopen((FILE_NAME + string(".in")).c_str(), "r", stdin);
  // freopen((FILE_NAME + string(".out")).c_str(), "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int t = 1;
#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  for (int i = 1; i <= t; ++i)
    solve(i);
}
