#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

/* #define MULTI 1 */

void pre()
{
}

void solve()
{
	int n, m; cin >> n >> m;
	int a[1500005]; for (int i = 0; i < n; ++i) cin >> a[i];
	int cnt[1500005] = {}; for (int i = 0; i < m; ++i) ++cnt[a[i]];
	priority_queue<int, vector<int>, greater<int>> pq;
	for (int i = 0; i < n; ++i) if (cnt[i] == 0) pq.push(i);
	pq.push(n);

	int ans = pq.top();
	for (int i = 0; i + m < n; ++i) {
		--cnt[a[i]];
		if (cnt[a[i]] == 0) pq.push(a[i]);
		++cnt[a[i + m]];
		while (cnt[pq.top()] > 0) pq.pop();
		ans = min(ans, pq.top());
	}
	cout << ans << endl;
}

int main()
{
	pre();
#ifdef MULTI
	int T;
	cin >> T;
	for (int t = 0; t < T; ++t) {
#endif
		solve();
#ifdef MULTI
	}
#endif
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
char s[11][11];
int ans;
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%s",s[i]+1); 
	for(int i=1;i<n;i++){
		int use=0;
		for(int j=1;j<=m;j++){
			if(s[i][j]!=s[i+1][j]){
				if(use==0) ans++;
				use=1;			
			} 	else use=0;
		}
	}
	for(int j=1;j<m;j++){
		int use=0;
		for(int i=1;i<=n;i++)
			if(s[i][j]!=s[i][j+1]){
				if(use==0) ans++;
				use=1;			
			} 	else use=0;
		}
	cout<<ans;
}