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

    int main()
    {
        ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        long long n, k;
        cin>>n>>k;
        map <int ,int>mp;
        int arr[n], mx = 0;
        for(int i=0 ; i<n; i++){
            cin>>arr[i];
            mx = max(mx, arr[i]);
        }
        if(k >= n) cout<<mx;
        else{
            for(int i=0; i<n;i++){
                for(int j =i+1 ; j < n+i; j++){
                    if(arr[i] > arr[j%n]) mp[arr[i]]++;
                    //cout<< mp[arr[i]]<<" ";
                    if(arr[i] < arr[j%n]) {mp[arr[j%n]]++ ; break;}
                    if(mp[arr[i]] == k) return cout<<arr[i] , 0;
                }
            }
        }
        return 0;
    }


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

    int main()
    {
        ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        long long n, k;
        cin>>n>>k;
        map <int ,int>mp;
        int arr[n], mx = 0;
        for(int i=0 ; i<n; i++){
            cin>>arr[i];
            mx = max(mx, arr[i]);
        }
        if(2*k >= n) cout<<mx;
        else{
            for(int i=0; i<n;i++){
                for(int j =i+1 ; j < n+i; j++){
                    if(arr[i] > arr[j%n]) mp[arr[i]]++;
                    else  {
                        mp[arr[j%n]]++ ;
                        break;
                        }
                    if(mp[arr[i]] == k) cout<<arr[i] ;
                }
            }
        }
        return 0;
    }


#include <bits/stdc++.h>

using namespace std;

int n, a[505];
long long k;

int main (){
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int u = 0;
    if (n <= k)
    {
        int max = a[1];
        for (int i = 1; i <= n; i ++)
            if (a[i] > max) max = a [i];
        cout << max;
    }
    else
    {
        for (int j = 1; j <= n; j++)
        {
        if (a[1] > a[2])
        {
            a[0] = a[2];
            for (int i = 2; i <= n; i++)
            a[i] = a[i+1];
            a[n] = a[0];
            u = u + 1;
        }
        else
        {
            a[0] = a[1];
            a[1] = a[2];
            for (int i = 2; i <= n; i++)
            a[i] = a[i+1];
            a[n] = a[0];
            u = 0;
        }
        if (u == k) break;
        }
    cout << a[1];
    }
}


#include <bits/stdc++.h>

using namespace std;

int n, a[505];
long long k;

int main (){
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int u = 0;
    if (n <= k)
    {
        int max = a[1];
        for (int i = 1; i <= n; i ++)
            if (a[i] > max) max = a [i];
    }
    else
    {
        for (int j = 1; j <= n; j++)
        if (a[1] > a[2])
        {
            a[0] = a[2];
            for (int i = 2; i <= n; i++)
            a[i] = a[i+1];
            a[n] = a[0];
            u = u + 1;
            if (u == k) break;
        }
        else
        {
            a[0] = a[1];
            a[1] = a[2];
            for (int i = 2; i <= n; i++)
            a[i] = a[i+1];
            a[n] = a[0];
            u = 0;
        }
    }
    cout << a[1];
}


#include <bits/stdc++.h>

using namespace std;

int n, a[505];
long long k;

int main (){
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int u = 0;
    if (n <= k)
    {
        int max = a[1];
        for (int i = 1; i <= n; i ++)
            if (a[i] > max) max = a [i];
        cout << max;
    }
    else
    {
        for (int j = 1; j <= n; j++)
        {
        if (a[1] > a[2])
        {
            a[0] = a[2];
            for (int i = 2; i <= n; i++)
            a[i] = a[i+1];
            a[n] = a[0];
            u = u + 1;
        }
        else
        {
            a[0] = a[1];
            a[1] = a[2];
            for (int i = 2; i <= n; i++)
            a[i] = a[i+1];
            a[n] = a[0];
            u = 1;
        }
        if (u == k) break;
        }
    cout << a[1];
    }
}


#include<cstdio>
using namespace std;
#define     N       10005

int i,j,k,l,n,a[N],t;
long long m;

int main()
{
    scanf("%d%I64d",&n,&m);
    for (i=1;i<=n;i++) scanf("%d",&a[i]);
    for (long long j=1;j<=m;j++) if (j<=(long long)n)
    {
        if (a[1]>a[2])
        {
            t=a[2];
            for (i=2;i<n;i++) a[i]=a[i+1];
            a[n]=t;
        }
        else
        {
            t=a[1];
            for (i=1;i<n;i++) a[i]=a[i+1];
            a[n]=t;
        }
    }
    else break;
    printf("%d\n",a[1]);
}


#include<cstdio>
using namespace std;
#define     N       10005

int i,j,k,l,n,m,a[N],t;

int main()
{
    scanf("%d%I64d",&n,&m);
    for (i=1;i<=n;i++) scanf("%d",&a[i]);
    for (long long j=1;j<=m;j++) if (j<=(long long)n)
    {
        if (a[1]>a[2])
        {
            t=a[2];
            for (i=2;i<n;i++) a[i]=a[i+1];
            a[n]=t;
        }
        else
        {
            t=a[1];
            for (i=1;i<n;i++) a[i]=a[i+1];
            a[n]=t;
        }
    }
    else break;
    printf("%d\n",a[1]);
}


#include<cstdio>
using namespace std;
#define     N       10005

int i,j,k,l,n,m,a[N],t;

int main()
{
    scanf("%d%I64d",&n,&m);
    for (i=1;i<=n;i++) scanf("%d",&a[i]);
    for (long long j=1;j<=m;j++) if (j<=n)
    {
        if (a[1]>a[2])
        {
            t=a[2];
            for (i=2;i<n;i++) a[i]=a[i+1];
            a[n]=t;
        }
        else
        {
            t=a[1];
            for (i=1;i<n;i++) a[i]=a[i+1];
            a[n]=t;
        }
    }
    else break;
    printf("%d",a[1]);
}


#include<cstdio>
using namespace std;
#define     N       10005

int i,j,k,l,n,m,a[N],t;

int main()
{
    scanf("%d%I64d",&n,&m);
    for (i=1;i<=n;i++) scanf("%d",&a[i]);
    for (long long j=1;j<=m;j++) if (j<=(long long)n)
    {
        if (a[1]>a[2])
        {
            t=a[2];
            for (i=2;i<n;i++) a[i]=a[i+1];
            a[n]=t;
        }
        else
        {
            t=a[1];
            for (i=1;i<n;i++) a[i]=a[i+1];
            a[n]=t;
        }
    }
    else break;
    printf("%d",a[1]);
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {
        e=max(e, x[i]);
        }
    if(w>=q-1)
        cout<<e;
    else
    {
        int i=0;
        while(k<q-1 && r<=w)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k>=q-1) {cout<<e; break;}
        r=1;
        i=k;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {w=e;
        e=max(x[i-1], x[i]);
        if(e>w) z=i+1;}
    if(w>=z-1)
        cout<<e;
    else
    {
        for(int i=0; i<q-1; i++)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k==q) {cout<<e; break;}
        r=0;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {
        e=max(x[i-1], x[i]);
        }
    if(w>=q-1)
        cout<<e;
    else
    {
        int i=0;
        while(k>0)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k>=q-1) {cout<<e; break;}
        r=1;
        i=k;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {u=e;
        e=max(x[i-1], x[i]);
        if(e>u) z=i+1;  }
    if(w>=z-1)
        cout<<e;
    else
    {
        for(int i=0; i<q-1; i++)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k==q) {cout<<e; break;}
        r=1;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    e=max(x[i-1], x[i]);
    if(w>=q-1)
        cout<<e;
    else
    {
        for(int i=0; i<q; i++)
        {k=i+1;
            while(x[i]>x[k] && k<q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k==q-1) {cout<<x[i]; break;}
        r=0;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {w=e;
        e=max(x[i-1], x[i]);
        if(e>w) z=i+1;}
    if(w>=z-1)
        cout<<e;
    else
    {
        for(int i=0; i<q-1; i++)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k==q) {cout<<e; break;}
        r=1;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {
        e=max(x[i-1], x[i]);
        }
    if(w>=q-1)
        cout<<e;
    else
    {
        int i=0;
        while(k<=q-1 && r<w)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k>=q-1) {cout<<e; break;}
        r=1;
        i=k;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {
        e=max(x[i-1], x[i]);
        }
    if(w>=q-1)
        cout<<e;
    else
    {
        int i=0;
        while(k<=q-1 && r<=w)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k>=q-1) {cout<<e; break;}
        r=1;
        i=k;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0]; cout<<e<<" ";
for(int i=1; i<q; i++)
    {
        e=max(e, x[i]); cout<<e<<" ";
        }
    if(w>=q-1)
        cout<<e;
    else
    {
        int i=0;
        while(k<q-1 && r<=w)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k>=q-1) {cout<<e; break;}
        r=1;
        i=k;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
    if(w==1)
e=x[0];
for(int i=1; i<q; i++)
    {
        e=max(x[i-1], x[i]);
        }
    if(w>=q-1)
        cout<<e;
    else
    {
        int i=0;
        while(k<q-1 && r<=w)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k>=q-1) {cout<<e; break;}
        r=1;
        i=k;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {w=e;
        e=max(x[i-1], x[i]);
        if(e>w) z=i;}
    if(w>=z-1)
        cout<<e;
    else
    {
        for(int i=0; i<q-1; i++)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k==q) {cout<<e; break;}
        r=0;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {
        e=max(x[i-1], x[i]);
        }
    if(w>=q-1)
        cout<<e;
    else
    {
        for(int i=0; i<q-1; i++)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k==q-1) {cout<<e; break;}
        r=1;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
e=x[0];
for(int i=1; i<q; i++)
    {u=e;
        e=max(x[i-1], x[i]);
        if(e>u) z=i+1;  }
    if(w>=z-1)
        cout<<e;
    else
    {
        for(int i=0; i<q-1; i++)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k==q-1) {cout<<e; break;}
        r=1;}
    }
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q, w,k, e=0, r=0, z, u;
cin>>q>>w;
long long x[q];
for(int i=0; i<q; i++)
    cin>>x[i];
    if(w==1)
e=x[0];
for(int i=1; i<q; i++)
    {
        e=max(x[i-1], x[i]);
        }
    if(w>=q-1)
        cout<<e;
    else
    {
        int i=0;
        while(k<=q-1 && r<=w)
        {k=i+1;
            while(x[i]>x[k] && k<=q-1)
        {
            r++; k++;
        }
        if(r>=w) {cout<<x[i]; break;}
        else if(k>=q-1) {cout<<e; break;}
        r=1;
        i=k;}
    }
}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
int ara[MX];
int main()
{
            //       freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              int mx=-INF;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                sc(ara[i]);
                mx=max(mx,ara[i]);
              }
              int curr=0,p1=1,p2=2;
               while(p1<=n||p2<=n)
               {

                    if(ara[p1]>ara[p2])
                    {
                        curr++;
                        p2++;
                    }
                    else
                    {
                        curr=1;
                        p1=p2;
                        p2=p1+1;
                    }
                    if(curr==k)
                    {
                        printf("%d\n",ara[p1]);
                        return 0;
                    }

               }
               printf("%d\n",mx);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
           //       freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
              for(int i=1;i<=n;)
              {
                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;
                      if(ara[i]>ara[j])
                      {
                          streak++;
                      }
                      else
                      {
                          dp[i]+=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(streak==k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }
                  }
                  if(i==n)break;
                      if(!flag)
                      {
                         dp[i]+=streak;
                            i++;
                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]==k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%lld%lld",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
            //      freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              int n,k;
              sc2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
              for(int i=1;i<=n;)
              {
                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;
                      if(ara[i]>ara[j])
                      {
                          streak++;
                      }
                      else
                      {
                          dp[i]=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(streak==k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }
                  }
                  if(i==n)break;
                      if(!flag)
                      {
                         dp[i]+=streak;
                            i++;
                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]==k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
             //     freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
              for(int i=1;i<=n;)
              {
                  if(dp[i]!=0)
                  {
                      i++;
                      continue;
                  }
                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;
                      if(ara[i]>ara[j])
                      {
                          streak++;
                      }
                      else
                      {
                          dp[i]+=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(streak==k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }

                  }
                  if(i==n)break;
                      if(!flag)
                      {
                         dp[i]+=streak;
                         if(dp[i]>=k)
                        {
                        Prll(ara[i]);
                          return 0;
                        }
                            i=1;

                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]>=k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
           //       freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
              for(int i=1;i<=n;)
              {
                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;
                      if(ara[i]>ara[j])
                      {
                          streak++;
                      }
                      else
                      {
                          dp[i]=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(streak==k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }
                  }
                  if(i==n)break;
                      if(!flag)
                      {
                         dp[i]+=streak;
                            i++;
                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]==k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
             //     freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
              mmset(dp,-1);
              for(int i=1;i<=n;)
              {
                  if(dp[i]!=-1)
                  {
                      i++;
                      continue;
                  }
                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  dp[i]=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;

                      if(ara[i]>ara[j])
                      {
                          dp[j]=0;
                          streak++;

                      }
                      else
                      {

                          dp[i]+=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(dp[i]>=k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }
                      if(dp[j]>=k)
                      {
                          Prll(ara[j]);
                          return 0;
                      }

                  }
                  if(i==n)break;
                      if(!flag)
                      {
                         dp[i]+=streak;
                         if(dp[i]>=k)
                        {
                        Prll(ara[i]);
                          return 0;
                        }
                            i=1;

                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]>=k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
            //      freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
            //  mmset(dp,-1);
              for(int i=1;i<=n;)
              {

                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;

                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;

                      if(ara[i]>ara[j])
                      {
                          dp[i]++;

                      }
                      else
                      {

                          //dp[i]+=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(dp[i]>=k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }

                  }
                 // if(i==n)break;
                      if(!flag)
                      {

                         if(dp[i]>=k)
                            {
                        Prll(ara[i]);
                          return 0;
                            }
                            i++;
                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 //  cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]>=k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
            //      freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
              for(int i=1;i<=n;)
              {
                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;
                      if(ara[i]>ara[j])
                      {
                          streak++;
                      }
                      else
                      {
                          dp[i]+=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(streak==k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }

                  }
                  if(i==n)break;
                      if(!flag)
                      {
                         dp[i]+=streak;
                            i++;
                      }
                      if(dp[i]>=k)
                      {
                        Prll(ara[i]);
                          return 0;
                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]>=k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
             //     freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
            //  mmset(dp,-1);
              for(int i=1;i<=n;)
              {

                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  dp[i]=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;

                      if(ara[i]>ara[j])
                      {
                          dp[i]++;

                      }
                      else
                      {

                          //dp[i]+=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(dp[i]>=k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }

                  }
                 // if(i==n)break;
                      if(!flag)
                      {

                         if(dp[i]>=k)
                            {
                        Prll(ara[i]);
                          return 0;
                            }
                            i++;
                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]>=k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
            //      freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
              for(int i=1;i<=n;)
              {
                  if(dp[i]!=-1)
                  {
                      i++;
                      continue;
                  }
                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  dp[i]=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;

                      if(ara[i]>ara[j])
                      {
                          dp[j]=0;
                          streak++;

                      }
                      else
                      {

                          dp[i]+=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(dp[i]>=k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }
                      if(dp[j]>=k)
                      {
                          Prll(ara[j]);
                          return 0;
                      }

                  }
                  if(i==n)break;
                      if(!flag)
                      {
                         dp[i]+=streak;
                         if(dp[i]>=k)
                        {
                        Prll(ara[i]);
                          return 0;
                        }
                            i=1;

                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]>=k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
int ara[MX];
int main()
{
             //      freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                sc(ara[i]);
              }
              int curr=0,mx=-INF,p1=1,p2=2;
               while(p1<=n||p2<=n)
               {
                    if(ara[p1]>ara[p2])
                    {
                        curr++;
                        p2++;
                    }
                    else
                    {
                        curr=1;
                        p1=p2;
                        p2=p1+1;
                    }
                    if(curr==k)
                    {
                        printf("%d\n",ara[p1]);
                        return 0;
                    }
                mx=max(mx,max(ara[p1],ara[p2]));
               }
               printf("%d\n",mx);

}


#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef                 long long ll;
typedef                 unsigned long long ull;
#define mkp             make_pair
#define pii             pair<int,int>
#define piii            pair<pii,int>
#define vi              vector<int>
#define PI              acos(-1)
#define all(v)          v.begin(),v.end()
#define mmset(a,val)    memset(a,val,sizeof(a))
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)    scanf("%d%d%d%d",&a,&b,&c,&d)
#define sc5(a,b,c,d,e)  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)
#define scll(a)         scanf("%I64d",&a)
#define scll2(a,b)      scanf("%I64d%I64d",&a,&b)
#define scll3(a,b,c)    scanf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)  scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)
#define Pr(a)           printf("%d\n",a)
#define Prll(a)         printf("%I64d\n",a)
#define pb(a)           push_back(a)
#define SZ              size()
#define tcs(t)          for(int tc=1;tc<=t;tc++)
#define INF             0x3f3f3f3f
#define hash_val        347
#define popcount        __builtin_popcount
#define MOD             1000000007
#define MX              505
//int fx[]={-1,-1,0,1,1,1,0,-1};
//int fy[]={0,1,1, 1,0,-1,-1,-1};
int Set(int n,int pos){return n=n|(1<<pos);}
int reset(int n,int pos){return n=n& ~ (1<<pos);}
bool check(int n,int pos){return (bool) (n& (1<<pos));}
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);};
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);};
ll ara[MX];
ll dp[MX];
int main()
{
             //     freopen("input.txt","r",stdin);
            //  freopen("output.txt","w",stdout);
              ll n,k;
              scll2(n,k);
              for(int i=1;i<=n;i++)
              {
                scll(ara[i]);
              }
              bool flag=0;
              mmset(dp,-1);
              for(int i=1;i<=n;)
              {

                  //cout<<i<<endl;
                  int streak=0;
                  flag=0;
                  dp[i]=0;
                  for(int j=i+1;j<=n;j++)
                  {
                     // cout<<i<<" "<<j<<endl;

                      if(ara[i]>ara[j])
                      {
                          dp[j]=0;
                          dp[i]++;

                      }
                      else
                      {

                          //dp[i]+=streak;
                          dp[j]=dp[i]+1;
                          i=j;
                          flag=1;
                          break;
                      }
                      if(dp[i]>=k)
                      {
                          Prll(ara[i]);
                          return 0;
                      }

                  }
                  if(i==n)break;
                      if(!flag)
                      {

                         if(dp[i]>=k)
                            {
                        Prll(ara[i]);
                          return 0;
                            }
                            i++;
                      }

              }
              ll mx=-INF,idx=-1;
              for(int i=1;i<=n;i++)
              {
                 // cout<<i<<" "<<dp[i]<<endl;
                  if(dp[i]>=k)
                  {
                      Prll(ara[i]);
                      return 0;
                  }
                 if(dp[i]>mx)
                 {
                     mx=dp[i];
                     idx=i;
                 }
              }
              Prll(ara[idx]);

}


/*input
7 2
1 4 3 6 5 2 7
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;//in(t);
    while(t--)
    {
        ll i,j,n,k,p=0;string s;
        in(n>>k);
        k=min(n-1,k);
        vi a(n);f(i,0,n)in(a[i]);
        i=0;
        int meh=1;
        while(1)
        {
            bool f=1;
            for(j=meh;j<=k;++j)
            {
                if(a[1]>a[0])
                {
                    int p=a[1];
                    a.erase(a.begin()+1);
                    a.pb(a[0]);
                    a[0]=p;f=0;
                    meh=2;
                    break;
                }
                else
                {
                    int p=a[1];
                    a.erase(a.begin()+1);
                    a.pb(p);
                }
                // f(i,0,n)
                //     cout<<a[i]<<" ";
                // out("");
            }
            if(f)
            {
                out(a[i]);rt 0;
            }
        }
    }
    rt 0;
}


/*input
6 2
6 5 3 1 2 4
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;//in(t);
    while(t--)
    {
        ll i,j,n,k,p=0;string s;
        in(n>>k);
        k=min(n-1,k);
        vi a(n);f(i,0,n)in(a[i]);
        i=0;
        while(1)
        {
            bool f=1;
            for(j=1;j<=k;++j)
            {
                if(a[j]>a[i])
                {
                    int p=a[j];
                    a.erase(a.begin()+j);
                    a.pb(a[i]);
                    a[i]=p;f=0;break;
                }
            }
            if(f && j==k+1)
            {
                out(a[i]);rt 0;
            }
        }
    }
    rt 0;
}


/*input
4 2
3 1 2 4
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;//in(t);
    while(t--)
    {
        ll i,j,n,k,p=0;string s;
        in(n>>k);
        k=min(n-1,k);
        vi a(n);f(i,0,n)in(a[i]);
        f(i,0,n)
        {
            bool f=1;
            for(j=i+1;j<=i+k;++j)
            {
                if(a[j%n]>a[i])
                {
                    f=0;break;
                }
            }
            if(f){p=a[i];break;}
        }
        out(p);
    }
    rt 0;
}


/*input
4 2
3 1 2 4
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;//in(t);
    while(t--)
    {
        ll i,j,n,k,p=0;string s;
        in(n>>k);
        k=min(n-1,k);
        vi a(n);f(i,0,n)in(a[i]);
        i=0;
        while(1)
        {
            bool f=1;
            for(j=1;j<=k;++j)
            {
                if(a[j]>a[i])
                {
                    int p=a[j];
                    a.erase(a.begin()+j);
                    a.pb(a[0]);
                    a[0]=p;f=0;break;
                }
                else
                {
                    int p=a[j];
                    a.erase(a.begin()+j);
                    a.pb(p);
                    //f(p,0,n)cout<<a[p]<<" ";out("");
                    ++j;
                }
            }
            if(f)
            {
                out(a[i]);rt 0;
            }
        }
    }
    rt 0;
}


/*input
4 2
3 1 2 4
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;//in(t);
    while(t--)
    {
        ll i,j,n,k,p=0;string s;
        in(n>>k);
        k=min(n-1,k);
        vi a(n);f(i,0,n)in(a[i]);
        f(i,0,n)
        {
            bool f=0;
            for(j=i-1;j>=0;--j)
            {
                if(a[j]<a[i])
                {
                    f=1;break;
                }
            }
            if(!i)f=1;
            if(!f)continue;
            for(j=i+1;j<=i+k;++j)
            {
                if(a[j%n]>a[i])
                {
                    f=0;break;
                }
            }
            if(f){p=a[i];break;}
        }
        out(p);
    }
    rt 0;
}


/*input
6 2
6 5 3 1 2 4
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;//in(t);
    while(t--)
    {
        ll i,j,n,k,p=0;string s;
        in(n>>k);
        k=min(n-1,k);
        vi a(n);f(i,0,n)in(a[i]);
        i=0;
        while(1)
        {
            bool f=1;
            for(j=i+1;j<=i+k;++j)
            {
                if(a[j%n]>a[i])
                {
                    int p=a[j%n];
                    a.erase(a.begin()+j%n);
                    a.pb(a[i]);
                    a[i]=p;f=0;break;
                    i=j%n;
                }
            }
            if(f)
            {
                out(a[i]);rt 0;
            }
        }
    }
    rt 0;
}


/*input
6 2
6 5 3 1 2 4
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;//in(t);
    while(t--)
    {
        ll i,j,n,k,p=0;string s;
        in(n>>k);
        k=min(n-1,k);
        vi a(n);f(i,0,n)in(a[i]);
        i=0;
        while(1)
        {
            bool f=1;
            for(j=1;j<=k;++j)
            {
                if(a[j]>a[i])
                {
                    int p=a[j];
                    a.erase(a.begin()+j);
                    a.pb(a[i]);
                    a[i]=p;f=0;break;
                }
                else
                {
                    int p=a[j];
                    a.erase(a.begin()+j);
                    a.pb(p);
                }
            }
            if(f)
            {
                out(a[i]);rt 0;
            }
        }
    }
    rt 0;
}


/*input
4 2
3 1 2 4
*/
#include <bits/stdc++.h>
#define pb push_back
#define epb emplace_back
#define mp make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define rt return
#define f(x,y,z) for(x=y;x<z;++x)
#define rev(x,y,z) for(x=z-1;x>=y;--x)
#define pf(x) printf("%d\n",x)
#define pfs(s) printf("%s\n",s)
#define out(s) cout<<s<<"\n"
#define in(s) cin>>s
#define st string
#define X first
#define Y second
using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vl;typedef vector<bool> vb;
ll modpow(ll x, ll y, ll m){ll z=1;while(y){if(y&1)z=(z*x)%m;x=(x*x)%m;y>>=1;}return z;}
int egcd(int a, int b, int& x, int& y){if(a == 0){x= 0;y= 1;return b;}int x1, y1;int gcd= egcd(b%a, a, x1, y1);x= y1 - (b/a) * x1;y= x1;return gcd;}
string z[2]={"NO","YES"};
ll mod=1e9+7;
// vector<vi> adj;vb v(1e5);
// void dfs(int s)
// {
//     v[s]=1;
//     int i;
//     f(i,0,adj[s].size())
//     {
//         if(!v[adj[s][i]])
//         {
//             dfs(adj[s][i]);
//         }
//     }
// }
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;//in(t);
    while(t--)
    {
        ll i,j,n,k,p=0;string s;
        in(n>>k);
        k=min(n-1,k);
        vi a(n);f(i,0,n)in(a[i]);
        i=0;
        while(1)
        {
            bool f=1;
            for(j=1;j<=k;++j)
            {
                if(a[1]>a[i])
                {
                    int p=a[1];
                    a.erase(a.begin()+1);
                    a.pb(a[0]);
                    a[0]=p;f=0;break;
                }
                else
                {
                    int p=a[1];
                    a.erase(a.begin()+1);
                    a.pb(p);
                    //f(p,0,n)cout<<a[p]<<" ";out("");
                }
            }
            if(f)
            {
                out(a[i]);rt 0;
            }
        }
    }
    rt 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int v;
int main ()
{
	int n,a[100001],c[100001],k,cnt;
	cin >> n >> k;
	int v = n;
	for ( int i = 1; i <= n; ++i )
	{
		cin >> a[i];
		v++;
		a[v+1] = a[i];
	}
	for ( int i = 1; i <= n; ++i )
	{
		cnt = a[i];
		for ( int j = i; j <= v; ++j )
		{
			if ( cnt > a[j] && j != i )
			{
				v++;
				if ( v >= k )
				{
					cout << a[i];
					return 0;
				}
				continue;
			}
			if ( cnt < a[j] )
			{
				break;
			}
		}
	}
	sort(a+1,a+1+n);
	cout << a[n];
}


#include <iostream>
#include <algorithm>
using namespace std;
int b;
int main ()
{
	long long n,a[100001],k,cnt;
	cin >> n >> k;
	int v = n*2;
	for ( int i = 1; i <= n; ++i )
	{
		cin >> a[i];
		a[v+1] = a[i];
	}
	for ( int i = 1; i <= n; ++i )
	{
		cnt = a[i];
		if ( a[i] > a[i-1] )
		{
			b++;
		}
		for ( int j = i; j <= v; ++j )
		{
			if ( cnt > a[j] )
			{
				b++;
				if ( b >= k || b >= n )
				{
					cout << a[i];
					return 0;
				}
			}
			if ( cnt < a[j] )
			{
				break;
			}
		}
		b = 0;
	}
	sort(a+1,a+1+n);
	cout << a[n];
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <sstream>
using namespace std;

#define DB(x) cerr<<#x<<"="<<x<<" "
#define DBN(x) cerr<<#x<<"="<<x<<"\n"
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

#define INF 1000000000
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462
int h, m, n;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

set<int >::iterator it;
map<int, pii > mm;
set<int > s[200000];
int p[200005];
int main(int argc, char *argv[])
{
    ios_base::sync_with_stdio(false);
    cin >> h >> m >> n;
    int g = gcd(h, m);
    for (int i = 0; i < g; i++) {
	for (int j = 0; j < h/g; j++) {
	    s[i].insert(j);
	    p[(i+(ll)j*m)%h] = j;
	}
    }
    long long ans = 0;
    while (n--) {
	int id, t;
	char op[10];
	cin >> op >> id;
	if (op[0] == '+') {
	    cin >> t;
	    it = s[t%g].lower_bound(p[t]);
	    if (it == s[t%g].end())
		it = s[t%g].begin();

	    mm[id] = mp(t, *it);

	    int count = (*it)-p[t];
	    if (count < 0)
		count += h/g;

	    ans += count;

	    if (it == s[t%g].end())
		printf("error!\n");
	    s[t%g].erase(it);
	}
	else {
	    s[mm[id].first%g].insert(mm[id].second);
	}
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <sstream>
using namespace std;

#define DB(x) cerr<<#x<<"="<<x<<" "
#define DBN(x) cerr<<#x<<"="<<x<<"\n"
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

#define INF 1000000000
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462
int h, m, n;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

set<int >::iterator it;
map<int, pii > mm;
set<int > s[200000];
int p[200005];
int main(int argc, char *argv[])
{
    cin >> h >> m >> n;
    int g = gcd(h, m);
    for (int i = 0; i < g; i++) {
	for (int j = 0; j < h/g; j++) {
	    s[i].insert(j);
	    p[(long long)(i+j*m)%h] = j;
	}
    }
    long long ans = 0;
    while (n--) {
	int id, t;
	char op[10];
	cin >> op >> id;
	if (op[0] == '+') {
	    cin >> t;
	    it = s[t%g].lower_bound(p[t]);
	    if (it == s[t%g].end())
		it = s[t%g].begin();

	    mm[id] = mp(t, *it);

	    int count = (*it)-p[t];
	    if (count < 0)
		count += h/g;

	    ans += count;

	    if (it == s[t%g].end())
		printf("error!\n");
	    s[t%g].erase(it);
	}
	else {
	    s[mm[id].first%g].insert(mm[id].second);
	}
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <sstream>
using namespace std;

#define DB(x) cerr<<#x<<"="<<x<<" "
#define DBN(x) cerr<<#x<<"="<<x<<"\n"
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

#define INF 1000000000
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462
int h, m, n;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}
set<int> s[200000];
set<int>::iterator it;
map<int, int> mm;
int main(int argc, char *argv[])
{
    cin >> h >> m >> n;
    int g = gcd(h, m);
    for (int i = 0; i < g; i++) {
	for (int j = 0; i + j < h; j += g)
	    s[i].insert(i+j);
    }
    long long ans = 0;
    while (n--) {
	int id, t;
	char op[10];
	cin >> op >> id;
	if (op[0] == '+') {
	    cin >> t;
	    it = s[t%g].lower_bound(t);
	    if (it == s[t%g].end())
		it = s[t%g].begin();
	    if (it == s[t%g].end()) {
		cout << "error\n";
		return 0;
	    }
	    mm[id] = *it;
	    int count = (*it)-t;
	    if (count < 0)
		count += h;
	    ans += count/m;
	    s[t%g].erase(it);
	}
	else {
	    s[mm[id]%g].insert(mm[id]);
	}
	DBN(ans);
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <sstream>
using namespace std;

#define DB(x) cerr<<#x<<"="<<x<<" "
#define DBN(x) cerr<<#x<<"="<<x<<"\n"
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

#define INF 1000000000
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462
int h, m, n;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}
set<int> s[200000];
set<int>::iterator itl, itr, it;
map<int, int> mm;
int main(int argc, char *argv[])
{
    cin >> h >> m >> n;
    int g = gcd(h, m);
    for (int i = 0; i < g; i++) {
	for (int j = 0; i + j < h; j += g)
	    s[i].insert(i+j);
    }
    long long ans = 0;
    cerr << "done!\n";
    while (n--) {
	int id, t;
	char op[10];
	cin >> op >> id;
	if (op[0] == '+') {
	    cin >> t;
	    it = s[t%g].lower_bound(t);
	    if (it == s[t%g].end())
		it = s[t%g].begin();
	    if (it == s[t%g].end()) {
		cerr << "error\n";
	    }
	    mm[id] = *it;
	    int count = (*it)-t;
	    if (count < 0)
		count += h;
	    ans += count/m;
	    s[t%g].erase(it);
	}
	else {
	    s[mm[id]%g].insert(mm[id]);
	}
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <sstream>
using namespace std;

#define DB(x) cerr<<#x<<"="<<x<<" "
#define DBN(x) cerr<<#x<<"="<<x<<"\n"
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

#define INF 1000000000
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462
int h, m, n;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

set<int >::iterator it;
map<int, pii > mm;
set<int > s[200000];
int p[200005];
int main(int argc, char *argv[])
{
    cin >> h >> m >> n;
    int g = gcd(h, m);
    for (int i = 0; i < g; i++) {
	for (int j = 0; j < h/g; j++) {
	    s[i].insert(j);
	    p[(i+(ll)j*m)%h] = j;
	}
    }
    long long ans = 0;
    while (n--) {
	int id, t;
	char op[10];
	cin >> op >> id;
	if (op[0] == '+') {
	    cin >> t;
	    it = s[t%g].lower_bound(p[t]);
	    if (it == s[t%g].end())
		it = s[t%g].begin();

	    mm[id] = mp(t, *it);

	    int count = (*it)-p[t];
	    if (count < 0)
		count += h/g;

	    ans += count;

	    if (it == s[t%g].end())
		printf("error!\n");
	    s[t%g].erase(it);
	}
	else {
	    s[mm[id].first%g].insert(mm[id].second);
	}
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <sstream>
using namespace std;

#define DB(x) cerr<<#x<<"="<<x<<" "
#define DBN(x) cerr<<#x<<"="<<x<<"\n"
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

#define INF 1000000000
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462
int h, m, n;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

set<int >::iterator it;
map<int, pii > mm;
set<int > s[200000];
int p[200005];
int main(int argc, char *argv[])
{
    cin >> h >> m >> n;
    int g = gcd(h, m);
    for (int i = 0; i < g; i++) {
	for (int j = 0; j < h/g; j++) {
	    s[i].insert(j);
	    p[(i+j*m)%h] = j;
	}
    }
    long long ans = 0;
    while (n--) {
	int id, t;
	char op[10];
	cin >> op >> id;
	if (op[0] == '+') {
	    cin >> t;
	    it = s[t%g].lower_bound(p[t]);
	    if (it == s[t%g].end())
		it = s[t%g].begin();

	    mm[id] = mp(t, *it);

	    int count = (*it)-p[t];
	    if (count < 0)
		count += h/g;

	    ans += count;

	    if (it == s[t%g].end())
		printf("error!\n");
	    s[t%g].erase(it);
	}
	else {
	    s[mm[id].first%g].insert(mm[id].second);
	}
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <sstream>
using namespace std;

#define DB(x) cerr<<#x<<"="<<x<<" "
#define DBN(x) cerr<<#x<<"="<<x<<"\n"
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

#define INF 1000000000
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462
int h, m, n;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

set<pii >::iterator it;
map<int, pii > mm;
set<pii > s[200000];
int p[200005];
int main(int argc, char *argv[])
{
    cin >> h >> m >> n;
    int g = gcd(h, m);
    for (int i = 0; i < g; i++) {
	for (int j = 0; j < h/g; j++) {
	    s[i].insert(mp(j, (i+j*m)%h));
	    p[(i+j*m)%h] = j;
	}
    }
    long long ans = 0;
    while (n--) {
	int id, t;
	char op[10];
	cin >> op >> id;
	if (op[0] == '+') {
	    cin >> t;
	    it = s[t%g].lower_bound(make_pair(p[t], t));
	    if (it == s[t%g].end())
		it = s[t%g].begin();
	    mm[id] = *it;
	    int count = (*it).first-p[t];
	    if (count < 0)
		count += h/g;
	    ans += count;
	    s[t%g].erase(it);
	}
	else {
	    s[mm[id].second%g].insert(mm[id]);
	}
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <sstream>
using namespace std;

#define DB(x) cerr<<#x<<"="<<x<<" "
#define DBN(x) cerr<<#x<<"="<<x<<"\n"
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

#define INF 1000000000
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462
int h, m, n;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

set<int >::iterator it;
map<int, pii > mm;
set<int > s[200000];
int p[200005];
int main(int argc, char *argv[])
{
    cin >> h >> m >> n;
    int g = gcd(h, m);
    for (int i = 0; i < g; i++) {
	for (int j = 0; j < h/g; j++) {
	    s[i].insert(j);
	    p[((ll)i+j*m)%h] = j;
	}
    }
    long long ans = 0;
    while (n--) {
	int id, t;
	char op[10];
	cin >> op >> id;
	if (op[0] == '+') {
	    cin >> t;
	    it = s[t%g].lower_bound(p[t]);
	    if (it == s[t%g].end())
		it = s[t%g].begin();

	    mm[id] = mp(t, *it);

	    int count = (*it)-p[t];
	    if (count < 0)
		count += h/g;

	    ans += count;

	    if (it == s[t%g].end())
		printf("error!\n");
	    s[t%g].erase(it);
	}
	else {
	    s[mm[id].first%g].insert(mm[id].second);
	}
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <ctime>
#include <cassert>
#include <list>
#include <bitset>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define forit(v,it) for(typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;
int n,m,h,id,hash,ans;
char ch;
bool u[300000];
map <int,int> Mp;
int main ()
{
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
scanf("%d%d%d",&h,&m,&n);
for(int i=0;i<n;++i)
{
scanf("\n%c %d",&ch,&id);
if(ch == '-')
{
u[Mp[id]] = false;
Mp[id] = 0;
continue;
}
scanf("%d",&hash);
for(int j=0;j<h;++j)
{
if(!u[(hash + j*m)%h])
{
ans += j;
u[(hash + j*m)%h] = true;
Mp[id] = (hash + j*m)%h;
break;
}
}
}
printf("%d",ans);
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
int H, M, N;
set<int> a[200005];
int p[200005], seq[200005], r=0;
int u[200005];
map<int, pair<int, int> > history;
void release(pair<int, int> P)
{
a[P.first].insert(P.second);
}
int main(void)
{
scanf("%d%d%d", &H, &M, &N);
for(int i=0;i<H;i++)
{
if(!u[i])
{
for(int c=0, j=i;!u[j];j=(j+M)%H, ++c)
{
p[j] = r; seq[j] = c;
a[r].insert(c);
u[j] = 1;
}
++r;
}
}
long long ans = 0;
while(N--)
{
char x[10];
int id, hash;
scanf("%s%d", x, &id);
if(x[0] == '-') release(history[id]);
else
{
scanf("%d", &hash);
hash %= H;
int A = p[hash], B = seq[hash];
set<int>::iterator it = a[A].lower_bound(B);
if(it == a[A].end())
{
it = a[A].begin();
ans += *it + H/r - B;
}
else
{
ans += *it - B;
}

history[id] = make_pair(A, *it);
a[A].erase(it);
}
}
printf("%I64d\n", ans);
return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#define N 2100
using namespace std;
int n,k,f[N][N],h[N],tot,i,l[N],j;
string s[N];
int dp(int l,int r)
{
if(l==r)return 0;
int X=++tot,i,j,mid=l+1,x,y;
for(i=l+1;i<=r;++i)if(h[i]<h[mid])mid=i;
x=dp(l,mid-1); y=dp(mid,r);
for(i=0;i<=mid-l;++i)
for(j=0;j<=r-mid+1;++j)f[X][i+j]=max(f[X][i+j],f[x][i]+f[y][j]+h[mid]*i*j);
return X;
}
int main()
{
scanf("%d%d",&n,&k);
for(i=1;i<=n;++i)cin>>s[i];
sort(s+1,s+n+1);
l[1]=s[1].length();
for(i=2;i<=n;++i)
{
l[i]=s[i].length();
for(j=0;j<l[i-1] && j<l[i] && s[i-1][j]==s[i][j];++j);
h[i]=j;
}
printf("%d\n",f[dp(1,n)][k]);
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ios>
#include <vector>
#include <map>

using namespace std;

typedef long long ll;

const int MAXN = 200100;

int h, m, n, d, z;
int place[MAXN];
map<int, int> ind;
vector<int> bit[MAXN];

int gcd(int a, int b) {
    if (a < b) swap(a, b);
    if (!b) return a;
    return gcd(b, a%b);
}

int query(int c, int i) {
    int ret = 0;
    for (i++; i>0; i-=i&-i) ret += bit[c][i];
    return ret;
}

void update(int c, int i, int v) {
    for (i++; i<bit[c].size(); i+=i&-i) bit[c][i] += v;
}

int main() {
    ios :: sync_with_stdio(0);

    if (fopen("input.txt", "r")) {
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    }

    cin >> h >> m >> n;
    d = gcd(h, m), z = h/d;
    for (int i=0; i<d; i++) {
        bit[i].resize(2*z+5, 0);
        int cur = i, cnt = 0;
        do {
            place[cur] = cnt++;
            cur = (cur+m)%h;
        } while (cur != i);
    }

    ll ans = 0;
    for (int i=0; i<n; i++) {
        char c; cin >> c;
        if (c == '+') {
            int id, hash;
            cin >> id >> hash;
            int x = hash%d, y = place[hash];
            int lo = y, hi = y+z-1;
            while (lo < hi) {
                int mid = (lo + hi) / 2;
                if (query(x, mid)-query(x, y-1) < mid-y+1) hi = mid;
                else lo = mid+1;
            }
            ans += lo-y;
            if (lo >= z) lo -= z;
            ind[id] = lo*d+x;
            update(x, lo, 1), update(x, lo+z, 1);
        } else {
            int id;
            cin >> id;
            int x = ind[id];
            update(x%d, x/d, -1), update(x%d, x/d+z, -1);
        }
    }
    cout << ans << '\n';
    return 0;
}


#define MAX 200005
#include <stdio.h>
#include <map>
#include <set>
using namespace std;
struct node{
    node(){}
    node(long long p1,long long p2){begin=p1,pos=p2;}
    long long begin,pos;
}arr[MAX];
bool stat[MAX];
map<long long,node>mp;
set<long long>S[MAX];
set<long long>::iterator it;
long long size[MAX];
int main(){
    long long h,m,n,res=0,i,id,t,j;
    node cur;
    char ch;
    scanf("%I64d%I64d%I64d",&h,&m,&n);
    for(i=0;i<h;++i){
        if(stat[i]) continue;
        for(j=0;;++j){
            if(stat[(i+j*m)%h]) break;
            stat[(i+j*m)%h]=true;
            arr[(i+j*m)%h]=node(i,j);
            S[i].insert(j);
            size[i]=j;
        }
    }
    for(i=1;i<=n;++i){
        scanf(" %c%I64d",&ch,&id);
        if(ch=='-'){
            cur=mp[id];
            S[cur.begin].insert(cur.pos);
        }else{
            scanf("%I64d",&t);
            cur=arr[t];
			//printf("%I64d\n",*(S[cur.begin].rbegin()));
            if(*(S[cur.begin].rbegin())<cur.pos){
                it=S[cur.begin].lower_bound(0);
                res+=size[cur.begin]-cur.pos+1+(*it);
                mp[id]=node(cur.begin,(*it));
                S[cur.begin].erase(it);
            }else{
                it=S[cur.begin].lower_bound(cur.pos);
                res+=(*it)-cur.pos;
                mp[id]=node(cur.begin,(*it));
                S[cur.begin].erase(it);
            }
        }
    }
    printf("%I64d\n",res);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

const int MAXN=200010;

int c[MAXN];
int n,m,h;
int col[MAXN],colcnt;
int id[MAXN],cnt;
int beg[MAXN],end[MAXN];
map<int,int> hash;

int getSum(int x)
{
    int ret=0;
    for(;x>0;x-=x&(-x))ret+=c[x];
    return ret;
}
void insert(int x,int val)
{
    for(;x<=h;x+=x&(-x))c[x]+=val;
}
int get(int X,int Y)
{
    int l=X,r=Y,mid;
    if(getSum(X)-getSum(X-1)==0)return X;
    while(r>l)
    {
        mid=(l+r+1)>>1;
        if(getSum(mid)-getSum(X-1)==mid-X+1)l=mid;
        else r=mid-1;
    }
    return l+1;
}
void init()
{
    scanf("%d%d%d",&h,&m,&n);
    cnt=1;
    colcnt=0;
    memset(col,-1,sizeof(col));
    for(int i=0;i<h;++i)
    {
        int x=i;
        if(col[x]==-1)
        {
            beg[colcnt]=end[colcnt]=cnt;
            while(col[x]==-1)
            {
                id[x]=cnt;
                col[x]=colcnt;
                end[colcnt]=cnt;
                x=(x+m)%h;
                cnt++;
            }
            colcnt++;
        }
    }
}
void solve()
{
    long long ans=0;
    int val,hashval;
    char cmd[2];
    memset(c,0,sizeof(c));
    for(int i=0;i<n;++i)
    {
        scanf("%s",cmd);
        if(cmd[0]=='+')
        {
            scanf("%d%d",&val,&hashval);
            int nowCol=col[hashval],tar;
            hashval=id[hashval];
            int L=beg[nowCol],R=end[nowCol];
//            cout<<hashval<<' '<<L<<' '<<R<<endl;
//            cout<<getSum(R)-getSum(hashval-1)<<endl;
            if(getSum(R)-getSum(hashval-1)==R-hashval+1)
            {
                ans+=R-hashval+1;
                tar=get(L,hashval-1);
                hash[val]=tar;
                ans+=tar-L;
                insert(tar,1);
            }
            else
            {
                tar=get(hashval,R);
                hash[val]=tar;
                ans+=tar-hashval;
                insert(tar,1);
            }
//            cout<<tar<<endl;
        }
        else
        {
            scanf("%d",&val);
            insert(hash[val],-1);
        }
    }
    cout<<ans<<endl;
}
int main()
{
//    freopen("in.txt","r",stdin);
    init();
    solve();
    return 0;
}


//============================================================================
// Author       : LAM PHAN VIET - lamphanviet@gmail.com
// Problem Name :
// Time Limit   : .000s
// Description  :
//============================================================================
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define REP(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define REPD(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define uint64 unsigned long long
#define int64 long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

#define BIT(n) (1<<(n))
#define AND(a,b) ((a) & (b))
#define OR(a,b) ((a) | (b))
#define XOR(a,b) ((a) ^ (b))
#define sqr(x) ((x) * (x))

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI 3.1415926535897932385
#define INF 1000111222
#define eps 1e-7
#define maxN 200111

int n, k, region[maxN], number[maxN], nRegion = 0, size[maxN];
set<int> freeSet[maxN];
map<int, ii> store;

int add(int id, int val) {
	int reg = region[val], num = number[val], pos;
	set<int>::iterator it = freeSet[reg].lower_bound(num);
	if (it == freeSet[reg].end()) pos = *freeSet[reg].begin();
	else pos = *it;

	store[id] = ii(reg, pos);
	freeSet[reg].erase(pos);
	if (num <= pos) return pos - num;
	return size[reg] - num + pos;
}

void remove(int id) {
	ii x = store[id];
	store.erase(id);
	freeSet[x.ff].insert(x.ss);
}

int main() {
	#ifndef ONLINE_JUDGE
	freopen("test.inp", "r", stdin);
	//freopen("test.out", "w", stdout);
	#endif
	fill(region, -1);
	int64 res = 0;
	char ques;
	int m, id, val;
	scanf("%d %d %d", &n, &k, &m);
	for (int i = 0; i < n; i++)
		if (region[i] == -1) {
			int x = i, nNumber = 0;
			size[nRegion] = 0;
			while (region[x] == -1) {
				region[x] = nRegion;
				number[x] = ++nNumber;
				freeSet[nRegion].insert(nNumber);
				x = (x + k) % n;
				size[nRegion]++;
			}
			nRegion++;
		}
	while (m--) {
		scanf(" %c %d ", &ques, &id);
		if (ques == '+') {
			scanf(" %d ", &val);
			res += add(id, val % n);
		}
		else remove(id);
	}
	cout << res << endl;
	return 0;
}

// Copyright (C) 2012, LamPhanViet


//============================================================================
// Author       : LAM PHAN VIET - lamphanviet@gmail.com
// Problem Name :
// Time Limit   : .000s
// Description  :
//============================================================================
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define REP(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define REPD(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define uint64 unsigned long long
#define int64 long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

#define BIT(n) (1<<(n))
#define AND(a,b) ((a) & (b))
#define OR(a,b) ((a) | (b))
#define XOR(a,b) ((a) ^ (b))
#define sqr(x) ((x) * (x))

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI 3.1415926535897932385
#define INF 1000111222
#define eps 1e-7
#define maxN 200111

int n, k, region[maxN], number[maxN], nRegion = 0, size[maxN];
set<int> freeSet[maxN];
map<int, ii> store;

int add(int id, int val) {
	int reg = region[val], num = number[val], pos;
	set<int>::iterator it = freeSet[reg].lower_bound(num);
	if (it == freeSet[reg].end()) pos = *freeSet[reg].begin();
	else pos = *it;

	store[id] = ii(reg, pos);
	freeSet[reg].erase(pos);
	if (num <= pos) return pos - num;
	return size[reg] - num + pos;
}

void remove(int id) {
	ii x = store[id];
	store.erase(id);
	freeSet[x.ff].insert(x.ss);
}

int main() {
	#ifndef ONLINE_JUDGE
	freopen("test.inp", "r", stdin);
	//freopen("test.out", "w", stdout);
	#endif
	fill(region, -1);
	char ques;
	int m, id, val, res = 0;
	scanf("%d %d %d", &n, &k, &m);
	for (int i = 0; i < n; i++)
		if (region[i] == -1) {
			int x = i, nNumber = 0;
			size[nRegion] = 0;
			while (region[x] == -1) {
				region[x] = nRegion;
				number[x] = ++nNumber;
				freeSet[nRegion].insert(nNumber);
				x = (x + k) % n;
				size[nRegion]++;
			}
			nRegion++;
		}
	while (m--) {
		scanf(" %c %d ", &ques, &id);
		if (ques == '+') {
			scanf(" %d ", &val);
			res += add(id, val % n);
		}
		else remove(id);
	}
	printf("%d\n", res);
	return 0;
}

// Copyright (C) 2012, LamPhanViet


#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<map>
#define N 250000
using namespace std;
map <int,int> id;
map <int,int> q[N];
long long ans;
int i,j,k,l,n,m,h,be[N],X[N],Y[N],num,pos[N],M,x,y;
char ch[10];
int main(){
	scanf("%d%d%d",&h,&m,&n);
	for(int i=0;i<h;++i)if(!be[i]){
		int j=i;
		num=0;
		++M;
		while(!be[j]){
			num++;
			be[j]=M;
			pos[j]=num;
			j=(j+m)%h;
			q[M][num]=1;
		}
	}
	for(int i=1;i<=n;++i){
		scanf("%s",ch);
		if(ch[0]=='+'){
			scanf("%d%d",&x,&y);
			int res,tmp;
			map <int,int> :: iterator it=q[be[y]].lower_bound(pos[y]);
			if(it==q[be[y]].end())it=q[be[y]].lower_bound(0);
			tmp=it->first;
			if(tmp<pos[y])res=num-pos[y]+tmp;
			else res=tmp-pos[y];
			ans+=res;

			id[x]=i;
			q[be[y]].erase(it);
			X[i]=be[y];
			Y[i]=tmp;
		}else{
			scanf("%d",&x);
			y=id[x];
			q[X[y]][Y[y]]=1;
		}
	}
	printf("%I64d\n",ans);
}