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



int main(){
  int n,m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for(int i=0;i<m;++i){
    int a,b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }

  ll ans=0;

  for(int i=0;i<n;++i){
    vector<int> dist(n,-1);
    queue<int> que;
    dist[i]=0;
    que.push(i);
    while(!que.empty()){
      int v=que.front();
      que.pop();

      ans++;

      for(int nv : g[v]){
        if(dist[nv]!=-1)continue;

        dist[nv]=dist[v]+1;
        que.push(nv);
      }
    }
  }

  cout << ans << endl;
  return 0;
}

//#include <atcoder/all>
#include <bits/stdc++.h>
#include <math.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

int main()
{
    int N, M;
    cin >> N >> M;
    vector<int> A(N);
    rep(i, N)
    {
        cin >> A[i];
    }
    multiset<int> st;
    unordered_map<int, int> mp;
    for (int i = 0; i <= N; i++) {
        st.insert(i);
        mp[i]++;
    }

    int res = INF_32;
    int right = 0;
    int size = 0;
    for (int left = 0; left < N; left++) {
        while (right < N && size < M) {
            if (st.find(A[right]) != st.end()) {
                st.erase(st.find(A[right]));
            }
            mp[A[right]]--;
            size++;
            right++;
        }

        res = min(res, *st.begin());

        if (right == N)
            break;

        if (left == right)
            right++;
        else {
            if (mp[A[left]] == 0) // 足りないのはあと一個
                st.insert(A[left]);
            mp[A[left]]++;
            size--;
        }
    }

    cout << res << endl;
    return 0;
}
