#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int main(){
  int N,M;
  cin>>N>>M;
  int a[N];
  rep(i,N){
    cin>>a[i];
  }
  int mex = MOD;
  vector <int> b(M);
  rep(i,M){
    b[i] = a[i];
  }
  sort(all(b));
  if(b[0]!=0){
    mex=0;
  }
  else{
    bool p = 0;
    for(int i = 1;i<M;i++){
      if(b[i]-b[i-1]>1){
        mex = b[i-1]+1;
        p = 1;
        break;
      }
    }
    if(p == 0){
      mex = b[M-1]+1;
    }
  }
  int kazu[1500100];
  rep(i,M){
    kazu[a[i]]++;
  }
 // cout << mex << endl;
  for(int i = M;i<N;i++){
    kazu[a[i]]++;
    kazu[a[i-M]]--;
    if(kazu[a[i-M]] == 0){
    //  cout << a[i-M] << endl;
      chmin(mex,a[i-M]);
    }
  }
  cout << mex << endl;
  return 0;  
}
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  string S, T;
  cin >> S >> T;
  
  int counts = 0, countt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '0') counts++;
    if (T[i] == '0') countt++;
  }
  if (counts != countt) {
    cout << -1 << endl;
    return 0;
  }
  
  int ms = 0, mt = 0, count = 0;
  for (int i = 0; i < counts; i++) {
    while (S[ms] == '1') ms++;
    while (T[mt] == '1') mt++;
    if (ms != mt) count++;
    ms++;
    mt++;
  }
  
  cout << count << endl;

  return 0;
}