#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#define endl '\n'
using namespace std;
 
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF=1e10+1;

vector<int> graph[2001];
bool visit[2001];

void dfs(int root) {
    if(visit[root]) return;
    visit[root]=1;
    for(auto node : graph[root]) {
        dfs(node);
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen("input.txt","r",stdin);
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++) {
        int a,b;
        cin>>a>>b;
        graph[a].push_back(b);
    }
    int ans=0;
    for(int i=1;i<=n;i++) {
        memset(visit,0,sizeof(visit));
        dfs(i);
        for(int j=1;j<=n;j++) {
            if(visit[j]) ans++;
        }
    }
    cout<<ans;
    return 0;
}
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;
//const int mod = 998244353;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];
    int now = 0;
    set<int> st, stt;
    rep(i, 0, 1500005) stt.insert(i);
    vector<int> mp(1500005);
    int mn = INF;
    rep(i, 0, n - m + 1){
        while(now < i + m){
            if(mp[a[now]] == 0) stt.erase(a[now]);
            if(mp[a[now]] == 0) st.insert(a[now]);
            mp[a[now]]++;
            now++;
        }
        auto it = stt.begin();
        mn = min(mn, (int)*it);
        mp[a[i]]--;
        if(mp[a[i]] == 0){
            stt.insert(a[i]);
            st.erase(a[i]);
        }
    }
    cout << mn << endl;
    return 0;
}
