#include<iostream>
#include<string>
#include<cstring>
#include<unordered_map>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<tuple>
#include<map>
#include <new>
using namespace std;
void main()
{
	int n,i,k,c=0,num_of_moves=0;
	cin >> n>>k;
	vector<int> v(n+1),s(n+1);
	for (i = 0; i < n; i++)
	{
		cin >> v[i];
		s[i] = v[i];
	}
	while (true)
	{

		v.push_back(v[k - 1]);
		v.erase(v.begin() + 0);
		if (v == s)
		{
			cout << -1 << endl; break;
		}
		c=count(v.begin(), v.end(), v[0]);
		num_of_moves++;
		if (c == n)
		{
			cout << num_of_moves << endl; break;
		}
	}
}


#include<iostream>
#include<string>
#include<cstring>
#include<unordered_map>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<tuple>
#include<map>
#include <new>
using namespace std;
void main()
{
	int n,i,k,c=0,num_of_moves=0;
	cin >> n>>k;
	vector<int> v(n),s(n);
	for (i = 0; i < n; i++)
	{
		cin >> v[i];
		s[i] = v[i];
	}
	while (true)
	{
		c = count(v.begin(), v.end(), v[0]);
		if (c == n)
		{
			cout << num_of_moves << endl; break;
		}
		v.push_back(v[k-1]);
		v.erase(v.begin() + 0);
		if (v == s)
		{
			cout << -1 << endl; break;
		}
		num_of_moves++;
	}
}


#include<iostream>
#include<string>
#include<cstring>
#include<unordered_map>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<tuple>
#include<map>
#include <new>
using namespace std;
void main()
{
	int n,i,k,c=0,num_of_moves=0;
	cin >> n>>k;
	vector<int> v(n),s(n);
	for (i = 0; i < n; i++)
	{
		cin >> v[i];
		s[i] = v[i];
	}
	for (i = 0; i < k;i++)
	{
		c = count(v.begin(), v.end(), v[0]);
		if (c == n)
		{
			cout << num_of_moves << endl; return;
		}
		v.push_back(v[k-1]);
		v.erase(v.begin() + 0);
		if (v == s)
		{
			cout << -1 << endl; return;
		}
		num_of_moves++;
	}
	cout << -1 << endl;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<set>
#include<map>

using namespace std;

int main()
{
	int ok=0,n,a[100001],i,k;

	cin>>n>>k;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
	}

	for(i=k-1;i<n-1;++i)
	{
		if(a[i]!=a[i+1])
		{
			cout<<-1;
			return 0;
		}
	}

	while(k>0&&a[k]==a[k-1])
		-- k;

	cout<<k;

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<set>
#include<map>

using namespace std;

int main()
{
	int ok=0,n,a[100001],i,k;

	cin>>n>>k;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
	}

	for(i=k-1;i<n-1;++i)
	{
		if(a[i]!=a[i+1])
		{
			cout<<-1;
			return 0;
		}
	}

	cout<<n-k;

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<set>
#include<map>

using namespace std;

int main()
{
	int ok=0,n,a[100001],i,k;

	cin>>n>>k;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
	}

	for(i=k-1;i<n-1;++i)
	{
		if(a[i]!=a[i+1])
		{
			cout<<-1;
			return 0;
		}
	}

	k=n-1;
	while(k>0&&a[k]==a[k-1])
		-- k;

	cout<<k;

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<set>
#include<map>

using namespace std;

int main()
{
	int ok=0,n,a[100001],i,k;

	cin>>n>>k;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
	}

	for(i=k-1;i<n-1;++i)
	{
		if(a[i]!=a[i+1])
		{
			cout<<-1;
			return 0;
		}
	}

	k=n-1;
	while(k>=0&&a[k]==a[k-1])
		-- k;

	cout<<k;

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<set>
#include<map>

using namespace std;

int main()
{
	int ok=0,n,a[100001],i,k;

	cin>>n>>k;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
	}

	for(i=k;i<n-1;++i)
	{
		if(a[i]!=a[i+1])
		{
			cout<<-1;
			return 0;
		}
	}

	k=n-1;
	while(k>=0&&a[k]==a[k-1])
		-- k;

	cout<<k;

	return 0;
}


#include <iostream>

int main()
{
    int n, k;
    std::cin >> n >> k;
    int *arr = new int[n];
    for (int i = 0; i != n; ++i)
    {
        std::cin >> arr[i];
    }
    int cnt = 0;
    while (arr[0] > arr[n - 1])
    {
        arr[0] -= arr[k - 1];
        arr[n] += arr[k - 1];
        ++cnt;
    }
    for (int i = 1; i != n; ++i)
    {
        if (arr[i - 1] != arr[i])
        {
            std::cout << -1;
            return 0;
        }
    }
    std::cout << cnt;
    return 0;
}


#include <iostream>

bool IsEqual(int *arr, int n, int i)
{
    for (int j = 0; j < n - 1; ++j)
    {
        if (arr[i - 1] != arr[i])
            return false;
        else
            ++i;
    }
    return true;
}

int main()
{
    int n, k;
    std::cin >> n >> k;
    int *arr = new int[2 * n];
    for (int i = 0; i != n; ++i)
    {
        std::cin >> arr[i];
    }
    int j = 1, cnt = 0;
    for (; j < n ; ++j)
    {
        if (!IsEqual(arr, n, j))
        {
            arr[n - 1 + j] = arr[k - 1 + j - 1];
            ++cnt;
        }
        else
            break;
    }
    if (IsEqual(arr, n, j))
        std::cout << cnt;
    else
        std::cout << -1;
    return 0;
}


#include <iostream>

int main()
{
    int n, k, v;
    std::cin >> n >> k;

    int *arr = new int[n];
    for (int i = 0; i != n; ++i)
    {
        std::cin >> v;
        arr[i] = v;
    }
    for (int i = k; i != n; ++i)
    {
        if (arr[i - 1] != arr[i])
        {
            std::cout << -1;
            return 0;
        }
    }
    int i;
    for (i = k - 1; i != 0; --i)
    {
        if (arr[i - 1] != arr[i])
        {
            break;
        }
    }

    std::cout << i;
    return 0;
}


#include <iostream>

bool IsEqual(int *arr, int n, int i)
{
	for (int j = 0; j < n - 1; ++j)
	{
		if (arr[i - 1] != arr[i])
			return false;
		else
			++i;
	}
	return true;
}

int main()
{
	int n, k;
	std::cin >> n >> k;
	int *arr = new int[2 * n];
	for (int i = 0; i != n; ++i)
	{
		std::cin >> arr[i];
	}
	int j = 1, cnt = 0;
	for (; j < n ; ++j)
	{
		if (arr[j - 1] > arr[n - j] && !IsEqual(arr, n, j))
		{
			arr[n - 1 + j] = arr[k - 1 + j - 1];
			++cnt;
		}
		else
			break;
	}
	if (IsEqual(arr, n, j))
		std::cout << cnt;
	else
		std::cout << -1;
	return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int n, k, input, lastInput, index;

int main()
{
#ifdef AYMAN_PC
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin >> n >> k;
	cin >> lastInput;
	index = 0;
	for (size_t i = 1; i < n; i++)
	{
		cin >> input;
		if (input != lastInput)
		{
			index = i;
			lastInput = input;
		}
	}
	if (k >= ++index)
		cout << n - k;
	else
		cout << -1;
	return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int n, k, input, lastInput, index;

int main()
{
#ifdef AYMAN_PC
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin >> n >> k;
	cin >> lastInput;
	index = 0;
	for (size_t i = 1; i < n; i++)
	{
		cin >> input;
		if (input != lastInput)
		{
			index = i;
			lastInput = input;
		}
	}
	if (index == 0)
		cout << 0;
	else if (k >= ++index)
		cout << n - k;
	else
		cout << -1;
	return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int n, k, input, lastInput, index;

int main()
{
#ifdef AYMAN_PC
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin >> n >> k;
	cin >> lastInput;
	index = 0;
	for (size_t i = 1; i < n; i++)
	{
		cin >> input;
		if (input != lastInput)
		{
			index = i;
			lastInput = input;
		}
	}
	if (index == 0)
		cout << 0;
	else if (k >= index + 1)
		cout << index;
	else
		cout << -1;
	return 0;
}


# include <iostream>
# include <fstream>
# include <stdio.h>
# include <stdlib.h>
# include <vector>
# include <map>
# include <algorithm>
# include <cmath>
# include <cstring>
# include <deque>

# define SIZE 10

using namespace std;

int cnt;
deque<int> w;

int main(void)
{
#ifdef Local
    freopen("input.txt", "r+", stdin);
#endif
    int k;
    cin >> cnt >> k;
    w.resize(cnt);
    for(int i = 0; i < cnt; i++){
        cin >> w[i];
    }

    k--;
    int a = w[k];
    bool temp = false;
    int idx;
    for(int i = k + 1; i < cnt; i++){
        if(a != w[i]){
            temp = true;
            break;
        }
    }

    if(temp == false){
        cout << k << endl;
        return 0;
    }

    cout << -1 << endl;

    return 0;
}


# include <iostream>
# include <fstream>
# include <stdio.h>
# include <stdlib.h>
# include <vector>
# include <map>
# include <algorithm>
# include <cmath>
# include <cstring>
# include <deque>

# define SIZE 10

using namespace std;

int cnt;
deque<int> w;

int main(void)
{
#ifdef Local
	freopen("input.txt", "r+", stdin);
#endif
	int k;
	cin >> cnt >> k;
	w.resize(cnt);
	for(int i = 0; i < cnt; i++){
		cin >> w[i];
	}

	int a;
	bool temp;
	for(int i= 0 ; i < cnt; i++){
		a = w[i];
		temp = false;
		for(int j = i + 1; j < cnt; j++){
			if(a != j){
				temp = true;
				break;
			}
		}

		if(temp == false){
			cout << i << endl;
			return 0;
		}
	}

	cout << -1 << endl;

	return 0;
}


# include <iostream>
# include <fstream>
# include <stdio.h>
# include <stdlib.h>
# include <vector>
# include <map>
# include <algorithm>
# include <cmath>
# include <cstring>
# include <deque>

# define SIZE 10

using namespace std;

int cnt;
deque<int> w;

int main(void)
{
#ifdef Local
    freopen("input.txt", "r+", stdin);
#endif
    int k;
    cin >> cnt >> k;
    w.resize(cnt);
    for(int i = 0; i < cnt; i++){
        cin >> w[i];
    }

    k--;
    int a = w[k];
    bool temp = false;
    int idx;
    for(int i = k + 1; i < cnt; i++){
        if(a != w[i]){
            temp = true;
            break;
        }
    }

    int b = 0;
    for(int i = k -1; i >=0; i--){
        if(w[i] == a)
            b++;
    }

    if(temp == false){
        cout << k-b << endl;
        return 0;
    }

    cout << -1 << endl;

    return 0;
}


# include <iostream>
# include <fstream>
# include <stdio.h>
# include <stdlib.h>
# include <vector>
# include <map>
# include <algorithm>
# include <cmath>
# include <cstring>
# include <deque>

# define SIZE 10

using namespace std;

int cnt;
deque<int> w;

int main(void)
{
#ifdef Local
    freopen("input.txt", "r+", stdin);
#endif
    int k;
    cin >> cnt >> k;
    w.resize(cnt);
    for(int i = 0; i < cnt; i++){
        cin >> w[i];
    }

    int a;
    bool temp;
    for(int i= 0 ; i < cnt; i++){
        a = w[i];
        temp = false;
        for(int j = i + 1; j < cnt; j++){
            if(a != w[j]){
                temp = true;
                break;
            }
        }

        if(temp == false){
            cout << i << endl;
            return 0;
        }
    }

    cout << -1 << endl;

    return 0;
}


# include <iostream>
# include <fstream>
# include <stdio.h>
# include <stdlib.h>
# include <vector>
# include <map>
# include <algorithm>
# include <cmath>
# include <cstring>
# include <deque>

# define SIZE 10

using namespace std;

int cnt;
deque<int> w;

int main(void)
{
#ifdef Local
    freopen("input.txt", "r+", stdin);
#endif
    int k;
    cin >> cnt >> k;
    w.resize(cnt);
    for(int i = 0; i < cnt; i++){
        cin >> w[i];
    }

    k--;
    int a = w[k];
    bool temp = false;
    int idx;
    for(int i = k + 1; i < cnt; i++){
        if(a != w[i]){
            temp = true;
            break;
        }
    }

    int b = 0;
    for(int i = k -1; i >=0; i--){
        if(w[i] == a)
            b++;
        else
            break;
    }

    if(temp == false){
        cout << k-b << endl;
        return 0;
    }

    cout << -1 << endl;

    return 0;
}


#include "iostream"

int a[100500];

int main()
{
    int n,k;
    scanf ("%d %d", &n, &k);
    for (int i = 0; i < n; i++)
        scanf ("%d", &a[i]);
    bool good = true;
    bool goood = true;
    for (int i = k-1; i < n - 1; i++)
    {
        if (a[i+1] != a[i])
            good= false;
    }
    int index=0;
        if (good== true)
            {
                for(int i = k - 1; i > 0; i--)
                {
                    if (a[i-1] == a[i])
                        index++;
                    else
                        break;
                }
                 printf("%d", k - index);
            }
         else
             printf("-1");
    return 0;
}


#include "iostream"

int a[100500];

int main()
{
    int n,k;
    scanf ("%d %d", &n, &k);
    for (int i = 0; i < n; i++)
        scanf ("%d", &a[i]);
    bool good = true;
    bool goood = true;
    for (int i = k-1; i < n - 1; i++)
    {
        if (a[i+1] != a[i])
            good= false;
    }
    int index=0;
        if (good== true)
            {
                for(int i = k - 1; i > -1; i--)
                {
                    if (a[i] == a[i + 1])
                        index++;
                }
                 printf("%d", k - index);
            }
         else
             printf("-1");
    return 0;
}


#include "iostream"

int a[100500];

int main()
{
    int n,k;
    scanf ("%d %d", &n, &k);
    for (int i = 0; i < n; i++)
        scanf ("%d", &a[i]);
    bool good = true;
    for (int i = k-1; i < n - 1; i++)
    {
        if (a[i+1] != a[i])
            good= false;
    }
         if (good== true)
             printf("%d", k -1);
         else
             printf("-1");
    return 0;
}


#include "iostream"

int a[100500];

int main()
{
    int n,k;
    scanf ("%d %d", &n, &k);
    for (int i = 0; i < n; i++)
        scanf ("%d", &a[i]);
    bool good = true;
    bool goood = true;
    for (int i = k-1; i < n - 1; i++)
    {
        if (a[i+1] != a[i])
            good= false;
    }
    int index=0;
        if (good== true)
            {
                for(int i = k - 1; i > -1; i--)
                {
                    if (a[i+1] == a[i])
                        index++;
                    else
                        break;
                }
                 printf("%d", k - index);
            }
         else
             printf("-1");
    return 0;
}


#include "iostream"

int a[100500];

int main()
{
    int n,k;
    scanf ("%d %d", &n, &k);
    for (int i = 0; i < n; i++)
        scanf ("%d", &a[i]);
    bool good = true;
    bool goood = true;
    for (int i = k-1; i < n - 1; i++)
    {
        if (a[i+1] != a[i])
            good= false;
    }
    int index=0;
        if (good== true)
            {
                for(int i = k - 1; i > 0; i--)
                {
                    if (a[i-1] == a[i])
                        index++;
                    else
                        break;
                }
                 printf("%d", k - index - 1);
            }
         else
             printf("-1");
    return 0;
}


#include<iostream>
#include<map>
using namespace std;
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		int a[100001];
		for(int i=0;i<n;++i)
		{
			cin>>a[i];
		}
		int max=1;
		int sum=1;
		int maxi=0;
		for(int i=1;i<n;++i)
		{
			if(a[i]==a[i-1])
				sum++;
			else
				sum=1;
			if(sum>max)
			{
				max=sum;
				maxi=i;
			}
		}
		if(max>=n-k+1&&k<=maxi&&k>=maxi-max)
			cout<<n-max<<endl;
		else
			cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<map>
using namespace std;
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		int a[100001];
		for(int i=0;i<n;++i)
		{
			cin>>a[i];
		}
		int max=1;
		int sum=1;
		int maxi=0;
		for(int i=1;i<n;++i)
		{
			if(a[i]==a[i-1])
				sum++;
			else
				sum=1;
			if(sum>max)
			{
				max=sum;
				maxi=i;
			}
		}
		if(n-max<k)
			cout<<n-max<<endl;
		else
			cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<map>
using namespace std;
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		int a[100001];
		for(int i=0;i<n;++i)
		{
			cin>>a[i];
		}
		int max=0;
		int sum=1;
		int maxi=0;
		for(int i=1;i<n;++i)
		{
			if(a[i]==a[i-1])
				sum++;
			else
				sum=1;
			if(sum>max)
			{
				max=sum;
				maxi=i;
			}
		}
		if(n-max<k)
			cout<<n-max<<endl;
		else
			cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<map>
using namespace std;
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		int a[100001];
		for(int i=0;i<n;++i)
		{
			cin>>a[i];
		}
		int max=1;
		int sum=1;
		int maxi=1;
		for(int i=1;i<n;++i)
		{
			if(a[i]==a[i-1])
				sum++;
			else
				sum=1;
			if(sum>max)
			{
				max=sum;
				maxi=i;
			}
		}
		if(max>=n-k+1&&k<=maxi&&k>=maxi-max)
			cout<<n-max<<endl;
		else
			cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<map>
using namespace std;
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		int a[100001];
		for(int i=0;i<n;++i)
		{
			cin>>a[i];
		}
		int f=0;
		for(int i=k-1;i<n;++i)
		{
			if(a[i]!=a[k-1])
			{
				f=1;
				break;
			}
		}
		if(f)
			cout<<"-1"<<endl;
		else
		{
			int i=0;
			for(i=k-2;i>=0;--i)
			{
				if(a[i]!=a[k-1])
				{
					cout<<i+1<<endl;
					break;
				}
			}
			if(i==-1)
				cout<<"0"<<endl;
		}
	}
	return 0;
}


#include<iostream>
#include<map>
using namespace std;
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		int a[100001];
		for(int i=0;i<n;++i)
		{
			cin>>a[i];
		}
		int max=1;
		int sum=1;
		int maxi=0;
		for(int i=1;i<n;++i)
		{
			if(a[i]==a[i-1])
				sum++;
			else
				sum=1;
			if(sum>max)
			{
				max=sum;
				maxi=i;
			}
		}
		if(max>=n-k+1)
			cout<<n-max<<endl;
		else
			cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<map>
using namespace std;
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		int a[100001];
		for(int i=0;i<n;++i)
		{
			cin>>a[i];
		}
		int f=0;
		for(int i=k-1;i<n;++i)
		{
			if(a[i]!=a[k-1])
			{
				f=1;
				break;
			}
		}
		if(f)
			cout<<"-1"<<endl;
		else
		{
			int i=0;
			for(i=k-2;i>=0;--i)
			{
				if(a[i]!=a[k])
				{
					cout<<i+1<<endl;
					break;
				}
			}
			if(i==-1)
				cout<<"0"<<endl;
		}
	}
	return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

bool check ( vector < int > a )
{
    int c = a[1] ;
    for ( int i = 0 ; i < a.size() ; i++ )
        if ( a[i] != c && a[i] != 0 )
            return false ;
    return true ;
}

int main ()
{
    freopen ( "i.txt" , "r" , stdin ) ;
    int n , k , tmp , target ;
    scanf ( "%d %d" , &n , &k ) ;
    vector < int > v ;
    v.push_back ( 0 ) ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        v.push_back ( tmp ) ;
    }
    if ( check ( v ) ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    target = v[k] ;
    if ( k+1 == v.size() ){
        printf ( "%d\n" , n-1 ) ;
        return 0 ;
    }
    int z = 0 ;
    while ( true ){
        v.push_back ( v[k] ) ;
        v.erase(v.begin()+1) ;
        z++ ;
        if ( check ( v ) ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
        if ( v[k] == target ){
            printf ( "-1\n" ) ;
            return 0 ;
        }
    }
    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

bool check ( vector < int > a )
{
    int c = a[1] ;
    for ( int i = 0 ; i < a.size() ; i++ )
        if ( a[i] != c && a[i] != 0 )
            return false ;
    return true ;
}

int main ()
{
   // freopen ( "i.txt" , "r" , stdin ) ;
    int n , k , tmp , target ;
    scanf ( "%d %d" , &n , &k ) ;
    vector < int > v ;
    v.push_back ( 0 ) ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        v.push_back ( tmp ) ;
    }
    if ( check ( v ) ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    target = v[k] ;
    if ( k+1 == v.size() ){
        printf ( "%d\n" , n-1 ) ;
        return 0 ;
    }
    int z = 0 ;
    while ( true ){
        v.push_back ( v[k] ) ;
        v.erase(v.begin()+1) ;
        z++ ;
        if ( check ( v ) ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
        if ( v[k] == target ){
            printf ( "-1\n" ) ;
            return 0 ;
        }
    }
    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

vector < int > res , base ;
set < vector < int > > s ;

bool check ()
{
    for ( int i = 0 ; i < res.size() - 1 ; i++ ){
        if ( res[i] != res[i+1] ){
            return false ;
        }
    }
    return true ;
}

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;
    int n , k  , tmp ;
    scanf ( "%d %d" , &n , &k ) ;
    k-- ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        res.push_back ( tmp ) ;
    }
    if ( check() ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    base = res ;
    int z = 0 ;
    do{
        res.push_back ( res[k] ) ;
        res.erase ( res.begin() ) ;
        if ( s.find ( res ) == s.end() )
            s.insert ( res ) ;
        else
            break ;
        z++ ;
        if ( check () ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
    }while ( res != base ) ;
    printf ( "-1\n" ) ;

    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

int res[100000+10] ;

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;
    int n , k  , tmp ;
    scanf ( "%d %d" , &n , &k ) ;
    for ( int i = 1 ; i <= n ; i++ ){
        scanf ( "%d" , &res[i] ) ;
    }
    int i ;
    for( i = n ; i > 0; i--)
        if( res[i] != res[k])
            break;
    printf ( "%d\n" , i > k ? -1 : i ) ;
    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

bool check ( vector < int > a )
{
    int c = a[1] ;
    for ( int i = 0 ; i < a.size() ; i++ )
        if ( a[i] != c && a[i] != 0 )
            return false ;
    return true ;
}

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;
    int n , k , tmp , target ;
    scanf ( "%d %d" , &n , &k ) ;
    vector < int > v ;
    v.push_back ( 0 ) ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        v.push_back ( tmp ) ;
    }
    target = v[k] ;
    int z = 0 ;
    while ( true ){
        v.push_back ( v[k] ) ;
        v.erase(v.begin()+1) ;
        z++ ;
        if ( check ( v ) ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
        if ( v[k] == target ){
            printf ( "-1\n" ) ;
            return 0 ;
        }
    }
    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

vector < int > res , base ;
set < vector < int > > s ;

bool check ()
{
    for ( int i = 0 ; i < res.size() - 1 ; i++ ){
        if ( res[i] != res[i+1] ){
            return false ;
        }
    }
    return true ;
}

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;
    int n , k  , tmp ;
    scanf ( "%d %d" , &n , &k ) ;
    k-- ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        res.push_back ( tmp ) ;
    }
    if ( check() ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    base = res ;
    int z = 0 ;
    do{
        res.push_back ( res[k] ) ;
        res.erase ( res.begin() ) ;
        if ( s.find ( res ) != s.end() )
            s.insert ( res ) ;
        else
            break ;
        z++ ;
        if ( check () ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
    }while ( res != base ) ;
    printf ( "-1\n" ) ;

    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

bool check ( vector < int > a )
{
    int c = a[1] ;
    for ( int i = 0 ; i < a.size() ; i++ )
        if ( a[i] != c && a[i] != 0 )
            return false ;
    return true ;
}

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;
    int n , k , tmp , target ;
    scanf ( "%d %d" , &n , &k ) ;
    vector < int > v ;
    v.push_back ( 0 ) ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        v.push_back ( tmp ) ;
    }
    if ( check ( v ) ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    target = v[k] ;
    if ( k+1 == v.size() ){
        printf ( "%d\n" , n ) ;
        return 0 ;
    }
    int z = 0 ;
    while ( true ){
        v.push_back ( v[k] ) ;
        v.erase(v.begin()+1) ;
        z++ ;
        if ( check ( v ) ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
        if ( v[k] == target ){
            printf ( "-1\n" ) ;
            return 0 ;
        }
    }
    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

vector < int > res , base ;
set < vector < int > > s ;

bool check ()
{
    for ( int i = 0 ; i < res.size() - 1 ; i++ ){
        if ( res[i] != res[i+1] ){
            return false ;
        }
    }
    return true ;
}

bool findd ( int k , int index )
{
    for ( int i = 0 ; i < res.size() ; i++ )
        if ( res[i] == k && i != index )
            return false ;
    return true ;
}

int main ()
{
   // freopen ( "i.txt" , "r" , stdin ) ;
    int n , k  , tmp ;
    scanf ( "%d %d" , &n , &k ) ;
    k-- ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        res.push_back ( tmp ) ;
    }
    if ( check() ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    if ( k != res.size() - 1 && findd ( res[k] , k ) ){
        printf ( "-1\n" ) ;
        return 0 ;
    }
    base = res ;
    int z = 0 ;
    do{
        res.push_back ( res[k] ) ;
        res.erase ( res.begin() ) ;
        if ( res[k] == base[k] && z > res.size() )
            break ;
        z++ ;
        if ( check () ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
    }while ( res != base ) ;
    printf ( "-1\n" ) ;

    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

vector < int > res , base ;
set < vector < int > > s ;

bool check ()
{
    for ( int i = 0 ; i < res.size() - 1 ; i++ ){
        if ( res[i] != res[i+1] ){
            return false ;
        }
    }
    return true ;
}

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;
    int n , k  , tmp ;
    scanf ( "%d %d" , &n , &k ) ;
    k-- ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        res.push_back ( tmp ) ;
    }
    if ( check() ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    base = res ;
    int z = 0 ;
    do{
        res.push_back ( res[k] ) ;
        res.erase ( res.begin() ) ;
        if ( res[k] == base[k] && z > res.size() )
            break ;
        z++ ;
        if ( check () ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
    }while ( res != base ) ;
    printf ( "-1\n" ) ;

    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

vector < int > res , base ;
set < vector < int > > s ;

bool check ()
{
    for ( int i = 0 ; i < res.size() - 1 ; i++ ){
        if ( res[i] != res[i+1] ){
            return false ;
        }
    }
    return true ;
}

bool findd ( int k , int index )
{
    for ( int i = 0 ; i < res.size() ; i++ )
        if ( res[i] == k && i != index )
            return false ;
    return true ;
}

int main ()
{
   // freopen ( "i.txt" , "r" , stdin ) ;
    int n , k  , tmp ;
    scanf ( "%d %d" , &n , &k ) ;
    k-- ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        res.push_back ( tmp ) ;
    }
    if ( check() ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    if ( findd ( res[k] , k ) ){
        printf ( "-1\n" ) ;
        return 0 ;
    }
    base = res ;
    int z = 0 ;
    do{
        res.push_back ( res[k] ) ;
        res.erase ( res.begin() ) ;
        if ( res[k] == base[k] && z > res.size() )
            break ;
        z++ ;
        if ( check () ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
    }while ( res != base ) ;
    printf ( "-1\n" ) ;

    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

bool check ( vector < int > a )
{
    int c = a[1] ;
    for ( int i = 0 ; i < a.size() ; i++ )
        if ( a[i] != c && a[i] != 0 )
            return false ;
    return true ;
}

int main ()
{
   // freopen ( "i.txt" , "r" , stdin ) ;
    int n , k , tmp , target ;
    scanf ( "%d %d" , &n , &k ) ;
    vector < int > v ;
    v.push_back ( 0 ) ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        v.push_back ( tmp ) ;
    }
    if ( check ( v ) ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    target = v[k] ;
    if ( k+1 == v.size() ){
        printf ( "%d\n" , n ) ;
        return 0 ;
    }
    int z = 0 ;
    while ( true ){
        v.push_back ( v[k] ) ;
        v.erase(v.begin()+1) ;
        z++ ;
        if ( check ( v ) ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
        if ( v[k] == target ){
            printf ( "-1\n" ) ;
            return 0 ;
        }
    }
    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

bool check ( vector < int > a )
{
    int c = a[1] ;
    for ( int i = 0 ; i < a.size() ; i++ )
        if ( a[i] != c && a[i] != 0 )
            return false ;
    return true ;
}

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;
    int n , k , tmp , target ;
    scanf ( "%d %d" , &n , &k ) ;
    vector < int > v ;
    v.push_back ( 0 ) ;
    if ( n == 1 ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        v.push_back ( tmp ) ;
    }
    target = v[k] ;
    int z = 0 ;
    while ( true ){
        v.push_back ( v[k] ) ;
        v.erase(v.begin()+1) ;
        z++ ;
        if ( check ( v ) ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
        if ( v[k] == target ){
            printf ( "-1\n" ) ;
            return 0 ;
        }
    }
    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

vector < int > res , base ;
set < vector < int > > s ;

bool check ()
{
    for ( int i = 0 ; i < res.size() - 1 ; i++ ){
        if ( res[i] != res[i+1] ){
            return false ;
        }
    }
    return true ;
}

bool findd ( int k , int index )
{
    for ( int i = 0 ; i < res.size() ; i++ )
        if ( res[i] == k && i != index )
            return false ;
    return true ;
}

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;
    int n , k  , tmp ;
    scanf ( "%d %d" , &n , &k ) ;
    k-- ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        res.push_back ( tmp ) ;
    }
    if ( check() ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    if ( k == res.size() - 1 ){
        printf ( "%d\n" , res.size() - 1 ) ;
        return 0 ;
    }
    if ( findd ( res[k] , k ) ){
        printf ( "-1\n" ) ;
        return 0 ;
    }
    base = res ;
    int z = 0 ;
    do{
        res.push_back ( res[k] ) ;
        res.erase ( res.begin() ) ;
        if ( res[k] == base[k] && z > res.size() )
            break ;
        z++ ;
        if ( check () ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
    }while ( res != base ) ;
    printf ( "-1\n" ) ;

    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

bool check ( vector < int > a )
{
    int c = a[1] ;
    for ( int i = 0 ; i < a.size() ; i++ )
        if ( a[i] != c && a[i] != 0 )
            return false ;
    return true ;
}

int main ()
{
   // freopen ( "i.txt" , "r" , stdin ) ;
    int n , k , tmp , target ;
    scanf ( "%d %d" , &n , &k ) ;
    vector < int > v ;
    v.push_back ( 0 ) ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        v.push_back ( tmp ) ;
    }
    if ( check ( v ) ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    target = v[k] ;
    int z = 0 ;
    while ( true ){
        v.push_back ( v[k] ) ;
        v.erase(v.begin()+1) ;
        z++ ;
        if ( check ( v ) ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
        if ( v[k] == target ){
            printf ( "-1\n" ) ;
            return 0 ;
        }
    }
    return 0 ;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>

using namespace std ;

vector < int > res , base ;

bool check ()
{
    for ( int i = 0 ; i < res.size() - 1 ; i++ ){
        if ( res[i] != res[i+1] ){
            return false ;
        }
    }
    return true ;
}

int main ()
{
   // freopen ( "i.txt" , "r" , stdin ) ;
    int n , k  , tmp ;
    scanf ( "%d %d" , &n , &k ) ;
    k-- ;
    for ( int i = 0 ; i < n ; i++ ){
        scanf ( "%d" , &tmp ) ;
        res.push_back ( tmp ) ;
    }
    if ( check() ){
        printf ( "0\n" ) ;
        return 0 ;
    }
    base = res ;
    int z = 0 ;
    do{
        res.push_back ( res[k] ) ;
        res.erase ( res.begin() ) ;
        z++ ;
        if ( check () ){
            printf ( "%d\n" , z ) ;
            return 0 ;
        }
    }while ( res != base ) ;
    printf ( "-1\n" ) ;

    return 0 ;
}


#include <iostream>
using namespace std;

int main()
{
    int arr[int(1e5 + 1)];
    int n, k; cin >> n >> k; k --;
    for(int i = 0; i < n; i ++) cin >> arr[i];
    int kelement = arr[k];
    bool flag = false;
    for(int i = 0; i < n; i ++)
    {
        if(arr[i] != kelement)
        {
            flag = true;
        }
    }
    if(!flag)
    {
        cout << 0 << endl; return 0;
    }
    for(int i = k + 1; i < n; i ++)
    {
        if(arr[i] != kelement)
        {
            cout << -1 << endl; return 0;
        }
    }
    int idx;
    for(int i = 0; i < k; i ++)
    {
        if(arr[i] != kelement) idx = i;
    }
    cout << k - idx << endl;
}


#include <iostream>
#include <vector>
using namespace std;

int n, k;
vector<int> vec;
void operation()
{
	vec.push_back(vec[k]);
	n++;
	vector<int> newvec;
	for(int i = 1; i < n; i ++)
	{
		newvec.push_back(vec[i]);
	}
	n--;
	vec = newvec;
}
bool done()
{
	int num = vec[0];
	for(int i = 1; i < n; i ++)
	{
		if(num != vec[i]) return false;
	}
	return true;
}
int main()
{
	cin >> n >> k;
	k--;
	vec.resize(n);
	for(int i = 0; i < n; i ++) cin >> vec[i];
	for(int i = 0; i < 1e5; i ++)
	{
		operation();
		if(done())
		{
			cout << i + 1<< endl;
			return 0;
		}
	}
	cout << -1 << endl;
}


#include <iostream>
#include <vector>
using namespace std;

int n, k;
vector<int> vec;
void operation()
{
    vec.push_back(vec[k]);
    n++;
    vector<int> newvec;
    for(int i = 1; i < n; i ++)
    {
        newvec.push_back(vec[i]);
    }
    n--;
    vec = newvec;
}
bool done()
{
    int num = vec[0];
    for(int i = 1; i < n; i ++)
    {
        if(num != vec[i]) return false;
    }
    return true;
}
int main()
{
    cin >> n >> k;
    k--;
    vec.resize(n);
    for(int i = 0; i < n; i ++) cin >> vec[i];
    for(int i = 0; i < 1e4; i ++)
    {
        if(done())
        {
            cout << i<< endl;
            return 0;
        }
        operation();

    }
    cout << -1 << endl;
}


#include <iostream>
using namespace std;

int main()
{
    int arr[int(1e5 + 1)];
    int n, k; cin >> n >> k; k --;
    for(int i = 0; i < n; i ++) cin >> arr[i];
    int kelement = arr[k];
    for(int i = k + 1; i < n; i ++)
    {
        if(arr[i] != kelement)
        {
            cout << -1 << endl; return 0;
        }
    }
    int idx  = -1;
    for(int i = 0; i < k; i ++)
    {
        if(arr[i] != kelement) idx = i;
    }
    cout << idx + 1 << endl;
}


#include <iostream>
using namespace std;

int main()
{
    int arr[int(1e5 + 1)];
    int n, k; cin >> n >> k; k --;
    for(int i = 0; i < n; i ++) cin >> arr[i];
    int kelement = arr[k];
    for(int i = k + 1; i < n; i ++)
    {
        if(arr[i] != kelement)
        {
            cout << -1 << endl; return 0;
        }
    }
    int idx;
    for(int i = 0; i < k; i ++)
    {
        if(arr[i] != kelement) idx = i;
    }
    cout << idx + 1 << endl;
}


#include <iostream>
#include <vector>
using namespace std;

int n, k;
vector<int> vec;
void operation()
{
    vec.push_back(vec[k]);
    n++;
    vector<int> newvec;
    for(int i = 1; i < n; i ++)
    {
        newvec.push_back(vec[i]);
    }
    n--;
    vec = newvec;
}
bool done()
{
    int num = vec[0];
    for(int i = 1; i < n; i ++)
    {
        if(num != vec[i]) return false;
    }
    return true;
}
int main()
{
    cin >> n >> k;
    k--;
    vec.resize(n);
    for(int i = 0; i < n; i ++) cin >> vec[i];
    for(int i = 0; i < 1e3; i ++)
    {
        if(done())
        {
            cout << i<< endl;
            return 0;
        }
        operation();

    }
    cout << -1 << endl;
}


#include <iostream>
using namespace std;

int main()
{
	int arr[int(1e5 + 1)];
	int n, k; cin >> n >> k; k --;
	for(int i = 0; i < n; i ++) cin >> arr[i];
	int kelement = arr[k];
	for(int i = k + 1; i < n; i ++)
	{
		if(arr[i] != kelement)
		{
			cout << -1 << endl; return 0;
		}
	}
	int idx;
	for(int i = 0; i < k; i ++)
	{
		if(arr[i] != kelement) idx = i;
	}
	cout << k - idx << endl;
}


#include <iostream>
#include <vector>
using namespace std;

int n, k;
vector<int> vec;
void operation()
{
    vec.push_back(vec[k]);
    vec.erase(vec.begin());
}
bool done()
{
    int num = vec[0];
    for(int i = 1; i < n; i ++)
    {
        if(num != vec[i]) return false;
    }
    return true;
}
int main()
{
    cin >> n >> k;
    k--;
    vec.resize(n);
    for(int i = 0; i < n; i ++) cin >> vec[i];
    for(int i = 0; i < 1e5; i ++)
    {
        operation();
        if(done())
        {
            cout << i + 1<< endl;
            return 0;
        }
    }
    cout << -1 << endl;
}


#include <iostream>
#include <vector>
using namespace std;

int n, k;
vector<int> vec;
void operation()
{
    vec.push_back(vec[k]);
    n++;
    vector<int> newvec;
    for(int i = 1; i < n; i ++)
    {
        newvec.push_back(vec[i]);
    }
    n--;
    vec = newvec;
}
bool done()
{
    int num = vec[0];
    for(int i = 1; i < n; i ++)
    {
        if(num != vec[i]) return false;
    }
    return true;
}
int main()
{
    cin >> n >> k;
    k--;
    vec.resize(n);
    for(int i = 0; i < n; i ++) cin >> vec[i];
    for(int i = 0; i < 1e5; i ++)
    {
        if(done())
        {
            cout << i<< endl;
            return 0;
        }
        operation();

    }
    cout << -1 << endl;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES

#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <set>

using namespace std;

typedef long long int64;
#define INF 1234567890
#define prime 999979
#define FOR(a, b) for(int a = 0; a < b; ++a)
#define eps 1e-7

int n, k, a[100000];

int main()
{
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    cin >> n >> k;
    --k;
    FOR(i, n)
        cin >> a[i];
    int YES = 0;
    for(int i = k; i < n; ++i)
        if (a[i] != a[k]) ++YES;
    if (YES)
    {
        cout << -1;
        return 0;
    }
    for(int j = k; j >= 0; --j)
        if (a[j] != a[k])
        {
            cout << j + 1;
            break;
        }
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES

#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <set>

using namespace std;

typedef long long int64;
#define INF 1234567890
#define prime 999979
#define FOR(a, b) for(int a = 0; a < b; ++a)
#define eps 1e-7

int n, k, a[100000];

int main()
{
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    cin >> n >> k;
    --k;
    FOR(i, n)
        cin >> a[i];
    int YES = 0;
    for(int i = k; i < n; ++i)
        if (a[i] != a[k]) ++YES;
    if (YES)
    {
        cout << -1;
        return 0;
    }
    for(int j = k; j >= 0; --j)
        if (a[j] != a[k])
        {
            cout << j + 1;
            return 0;
        }
    cout << 0;
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES

#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <set>

using namespace std;

typedef long long int64;
#define INF 1234567890
#define prime 999979
#define FOR(a, b) for(int a = 0; a < b; ++a)
#define eps 1e-7

int n, k, a[100000];

int main()
{
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    cin >> n >> k;
    --k;
    FOR(i, n)
        cin >> a[i];
    int YES = 0;
    for(int i = k; i < n; ++i)
        if (a[i] != a[k]) ++YES;
    if (YES)
    {
        cout << -1;
        return 0;
    }
    for(int j = k; j >= 0; --j)
        if (a[j] != a[k])
        {
            cout << j + 1;
            break;
        }
    cout << 0;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>

using namespace std;
int w[100002]={};
int main() {
    int n,k;int f=0;
    set<int>x;
    set<int>y;
    cin>>n>>k;

    for(int i=1;i<=n;i++){

        int r;
        cin>>r;
        if(i<k)w[i]=r;
        else{y.insert(r);f=r;}
    }

    if(y.size()==1){
        for(int i=k-1;i>=1;i--){
        //  cout<<w[i]<<f;
            if(w[i]==f)continue;
            else {cout<<i;return 0;
            }
        }
        cout<<"0";return 0;
    }
    else cout<<"-1";return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>

using namespace std;
int w[100002]={};
int main() {
    int n,k;int f=0;
    set<int>x;
    set<int>y;
    cin>>n>>k;

    for(int i=1;i<=n;i++){

        int r;
        cin>>r;
        if(i<k)w[i]=r;
        else{y.insert(r);r=f;}
    }

    if(y.size()==1){
        for(int i=k;i>=1;i--){
            if(w[i]==f)continue;
            else {cout<<i;return 0;
            }
        }
        cout<<"0";return 0;
    }
    else cout<<"-1";return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>

using namespace std;
int w[100002]={};
int main() {
    int n,k;int f=0;
    set<int>x;
    set<int>y;
    cin>>n>>k;

    for(int i=1;i<=n;i++){

        int r;
        cin>>r;
        if(i<k)cin>>w[i];
        else{y.insert(r);r=f;}
    }

    if(y.size()==1){
        for(int i=k;i>=0;i--){
            if(w[i]==f)continue;
            else {cout<<i;return 0;
            }
        }
        cout<<k;return 0;
    }
    else cout<<"-1";return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>

using namespace std;
int w[100002]={};
int main() {
    int n,k;int f=0;
    set<int>x;
    set<int>y;
    cin>>n>>k;

    for(int i=1;i<=n;i++){

        int r;
        cin>>r;
        if(i<k)w[i]=r;
        else{y.insert(r);r=f;}
    }

    if(y.size()==1){
        for(int i=k;i>=1;i--){
            if(w[i]==f)continue;
            else {cout<<i;return 0;
            }
        }
        cout<<k;return 0;
    }
    else cout<<"-1";return 0;
}


#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    //qsort (values, 6, sizeof(int), compare);
    int n, k, count = 0;
    cin >> n >> k;
    int* arr = new int[n];
    for ( int j = 0; j < n; ++j )
        cin >> arr[j];
    int l = 0;
    for ( l = 0; l < n-1; ++l )
        if ( arr[l] != arr[l+1] )
            break;
    if ( l == n-1 )
        cout << 0;
    else
    {
        if ( k == 1 )
            cout << -1;
        else
        {
            if ( arr[k-1] == arr[n-1] )
            {
                for ( int j = n - 1; j >= 0; --j )
                {
                    if ( arr[j] == arr[k-1] )
                        ++count;
                    else
                        break;
                }
                cout << n-count;
            }
            else
                cout << -1;
        }

    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    //qsort (values, 6, sizeof(int), compare);
    int n, k, count = 0;
    cin >> n >> k;
    int* arr = new int[n];
    for ( int j = 0; j < n; ++j )
        cin >> arr[j];
    if ( k == 1 )
        cout << -1;
    else
    {
        if ( arr[k-1] == arr[n-1] )
        {
            for ( int j = n - 1; j >= 0; --j )
            {
                if ( arr[j] == arr[k-1] )
                    ++count;
                else
                    break;
            }
            cout << n-count;
        }
        else
            cout << -1;
    }
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

#define ll long long
#define ld long double

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    //qsort (arr, n, sizeof(int), compare);
	int n, k, count;
	cin >> n >> k;
	int arr[100001];
	for ( int j = 0; j < n; ++j )
		cin >> arr[j];
	for ( int j = 0; j < n; ++j )
		if ( arr[j] != arr[n-1] )
			count = j+1;
	if ( count >= k )
		cout << -1;
	else
		cout << count;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

#define ll long long
#define ld long double

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    //qsort (arr, n, sizeof(int), compare);
    int n, k, count = 0;
    cin >> n >> k;
    int arr[100001];
    for ( int j = 0; j < n; ++j )
        cin >> arr[j];
    for ( int j = k; j < n; ++j )
        if ( arr[j] != arr[j-1] )
        {
            cout << -1;
            return 0;
        }
    cout << n - k;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

#define ll long long
#define ld long double

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    //qsort (arr, n, sizeof(int), compare);
    int n, k, count = 0;
    cin >> n >> k;
    int arr[100001];
    for ( int j = 0; j < n; ++j )
        cin >> arr[j];
    int j = k;
    for ( j; j < n; ++j )
        if ( arr[j] != arr[j-1] )
        {
            cout << -1;
            return 0;
        }
    if ( j == n )
        cout << 0;
    else
        cout << n - k;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

#define ll long long
#define ld long double

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    //qsort (arr, n, sizeof(int), compare);
    int n, k, count = 0;
    cin >> n >> k;
    int arr[100001];
    for ( int j = 0; j < n; ++j )
        cin >> arr[j];
    int j = k;
    for ( j; j < n; ++j )
        if ( arr[j] != arr[j-1] )
        {
            cout << -1;
            return 0;
        }
    if ( j == n && k == 1 )
        cout << 0;
    else if ( n == k )
        cout << n-1;
    else
        cout << n - k;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

#define ll long long
#define ld long double

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    //qsort (arr, n, sizeof(int), compare);
    int n, k, count = 0;
    cin >> n >> k;
    int arr[100001];
    for ( int j = 0; j < n; ++j )
        cin >> arr[j];
    int j = k;
    for ( j; j < n; ++j )
        if ( arr[j] != arr[j-1] )
        {
            cout << -1;
            return 0;
        }
    if ( j == n && k == 1 )
        cout << 0;
    else
        cout << n - k;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

#define ll long long
#define ld long double

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main()
{
    //qsort (arr, n, sizeof(int), compare);
    int n, k, count = 0;
    cin >> n >> k;
    int arr[100001];
    for ( int j = 0; j < n; ++j )
        cin >> arr[j];
    for ( int j = 0; j < n; ++j )
        if ( arr[j] != arr[n-1] )
            count = j+1;
    if ( count >= k )
        cout << -1;
    else
        cout << count;
    return 0;
}


#include<stdio.h>
int main()
{
	int a[100001]={'-1'};
	int i,n,k,sum=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=k-1;i<n-1;i++)
	{
		if(a[i]==a[i+1])
			continue;
		else
		{
			printf("-1");
			goto lable;
		}
	}
	for(i=k-2;i>=0;i--)
	{
		if(a[i]==a[i+1])
			sum++;
		else break;
	}
	printf("%d",k-1-sum);
lable:	return 0;
}


#include<stdio.h>
int main()
{
	int a[100001]={'-1'};
	int i,n,k,sum=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=k-1;i<n-1;i++)
	{
		if(a[i]==a[i+1])
			continue;
		else
		{
			printf("-1");
			goto lable;
		}
	}
	for(i=k-2;i>=0;i--)
	{
		if(a[i]==a[k-1])
			sum++;
	}
	printf("%d",k-1-sum);
lable:	return 0;
}


#include<stdio.h>
int main()
{
	int a[100001]={'-1'};
	int i,n,k,sum=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=k-1;i<n-1;i++)
	{
		if(a[i]==a[i+1])
			continue;
		else
		{
			printf("-1");
			goto lable;
		}
	}
	for(i=k-2;i>=0;i--)
	{
		if(a[i]==a[i+1])
			sum++;
	}
	printf("%d",k-1-sum);
lable:	return 0;
}


#include<stdio.h>
int main()
{
	int a[100001]={'0'};
	int i,n,k,sum=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=k-1;i<n-1;i++)
	{
		if(a[i]==a[k-1])
			continue;
		else
		{
			printf("-1");
			goto lable;
		}
	}
	for(i=k-2;i>0;i--)
	{
		if(a[i]==a[k-1])
			sum++;
	}
	printf("%d",n-k-sum);
lable:	return 0;
}


#include<stdio.h>
int main()
{
	int a[100001]={'-1'};
	int i,n,k,sum=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=k-1;i<n-1;i++)
	{
		if(a[i]==a[k-1])
			continue;
		else
		{
			printf("-1");
			goto lable;
		}
	}
	for(i=k-2;i>=0;i--)
	{
		if(a[i]==a[k-1])
			sum++;
	}
	printf("%d",k-1-sum);
lable:	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <utility>
#include <fstream>
#include <cmath>
//#include <iomanip>

using namespace std;

int main()
{
   int n,k,kol=0;
   cin>>n>>k;
   vector<int> a(n);
   for(int i = 0; i < n; i++)
   {
        cin>>a[i];
   }
   for(int i=k;i<n;i++)
   {
    if(a[i]!=a[k-1])
    {
        cout<<"-1";
        return 0;
    }
   }
   for(int i=k-2;i>0;i--)
   {
    if(a[i]==a[k-1])
        kol++;
    else
        break;
   }
   cout<<k-1-kol;
   return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <utility>
#include <fstream>
#include <cmath>
//#include <iomanip>

using namespace std;

int main()
{
   int n,k,kol=0;
   cin>>n>>k;
   vector<int> a(n);
   for(int i = 0; i < n; i++)
   {
        cin>>a[i];
   }
   for(int i=k;i<n;i++)
   {
    if(a[i]!=a[k-1])
    {
        cout<<"-1";
        return 0;
    }
   }
   for(int i=k-2;i>=0;i--)
   {
    if(a[i]==a[k-1])
        kol++;
    else
        break;
   }
   cout<<kol<<endl;
   cout<<k-1-kol;
   return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <utility>
#include <fstream>
#include <cmath>
//#include <iomanip>

using namespace std;

int main()
{
   int n,k,kol=0;
   cin>>n>>k;
   vector<int> a(n);
   for(int i = 0; i < n; i++)
   {
        cin>>a[i];
   }
   for(int i=k;i<n;i++)
   {
    if(a[i]!=a[k-1])
    {
        cout<<"-1";
        return 0;
    }
   }
   for(int i=k-2;i>=0;i--)
   {
    if(a[i]==a[k-1])
        kol++;
    else
        break;
   }
   //cout<<kol<<endl;
   cout<<k-1-kol;
   return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
	    bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
		            return left.second < right.second;
			        }
};
int gcd (int a, int b) {
		return b ? gcd (b, a % b) : a;
}

int main()
{
	int n,k;
	SI(n);
	SI(k);
	vi v;
	int cnt = 0;
	bool flag = true;
	bool f3 = true;
	rep(i, n){
		int temp;
		SI(temp);
		v.PB(temp);
	}
	rep(i, v.size()){
		if(v[i]!=v[0])
		{
				f3 = false;
				break;
		}
	}
	if(f3){
		cout<<0<<endl;
		return 0;
	}
	vi v2 = v;
	vi v3;
	int o = 0;
	while(flag){
		v2.PB(v2[k-1]);
		v2.erase(v2.begin());
		if(v3 == v2){
			cout<<-1;
			return 0;
		}
		if(v != v2){
			cnt++;
		}
		else {
			cout<<-1<<endl;
			return 0;
		}
		bool f2 = true;

		rep(i, v2.size()){
			if(v2[i]!=v2[0])
			{
				f2 = false;
				break;
			}
		}
		if(f2){
			cout<<cnt<<endl;
			return 0;
		}
		if(o==25000){
			cout<<-1<<endl;
			return 0;
		}
		o++;

	}
	return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
        bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
                    return left.second < right.second;
                    }
};
int gcd (int a, int b) {
        return b ? gcd (b, a % b) : a;
}

int main()
{
    int n,k;
    SI(n);
    SI(k);
    vi v;
    int cnt = 0;
    bool flag = true;
    bool f3 = true;
    rep(i, n){
        int temp;
        SI(temp);
        v.PB(temp);
    }
    bool f4 = false;
    repx(i, k-1, v.size()){
        if(v[i]!= v[k-1])
            f4 = true;
    }
    if(f4)
    {
        cout<<-1<<endl;
        return 0;
    }
    rep(i, v.size()){
        if(v[i]!=v[0])
        {
                f3 = false;
                break;
        }
    }
    if(f3){
        cout<<0<<endl;
        return 0;
    }
    vi v2 = v;
    while(flag){
        cnt++;
        bool f2 = true;
        v2.PB(v2[k-1]);
        v2.erase(v2.begin());
        rep(i, v2.size()){
            if(v2[i]!=v2[0])
            {
                f2 = false;
                break;
            }
        }
        if(f2){
            cout<<cnt<<endl;
            return 0;
        }

    }
    return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
	    bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
		            return left.second < right.second;
			        }
};
int gcd (int a, int b) {
		return b ? gcd (b, a % b) : a;
}

int main()
{
	int n,k;
	SI(n);
	SI(k);
	vi v;
	int cnt = 0;
	bool flag = true;
	rep(i, n){
		int temp;
		SI(temp);
		v.PB(temp);
	}
	vi v2 = v;
	while(flag){
		v2.PB(v2[k-1]);
		v2.erase(v2.begin());
		if(v != v2){
			cnt++;
		}
		else {
			cout<<-1<<endl;
			return 0;
		}
		bool f2 = true;

		rep(i, v2.size()){
			if(v2[i]!=v2[0])
			{
				f2 = false;
				break;
			}
		}
		if(f2){
			cout<<cnt<<endl;
			return 0;
		}

	}
	return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
        bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
                    return left.second < right.second;
                    }
};
int gcd (int a, int b) {
        return b ? gcd (b, a % b) : a;
}

int main()
{
    int n,k;
    SI(n);
    SI(k);
    vi v;
    int cnt = 0;
    bool flag = true;
    bool f3 = true;
    rep(i, n){
        int temp;
        SI(temp);
        v.PB(temp);
    }
    rep(i, v.size()){
        if(v[i]!=v[0])
        {
                f3 = false;
                break;
        }
    }
    if(f3){
        cout<<0<<endl;
        return 0;
    }
    vi v2 = v;
    vi v3;
    while(flag){
        if(v3 == v2){
            cout<<-1;
            return 0;
        }
        v2.PB(v2[k-1]);
        v2.erase(v2.begin());
        if(v != v2){
            cnt++;
        }
        else {
            cout<<-1<<endl;
            return 0;
        }
        bool f2 = true;

        rep(i, v2.size()){
            if(v2[i]!=v2[0])
            {
                f2 = false;
                break;
            }
        }
        if(f2){
            cout<<cnt<<endl;
            return 0;
        }
        v3 = v2;

    }
    return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
        bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
                    return left.second < right.second;
                    }
};
int gcd (int a, int b) {
        return b ? gcd (b, a % b) : a;
}

int main()
{
    int n,k;
    SI(n);
    SI(k);
    vi v;
    int cnt = 0;
    int cnt2= 0;
    bool flag = true;
    bool f3 = true;
    rep(i, n){
        int temp;
        SI(temp);
        v.PB(temp);

    }
    rep(i, v.size()){
        if(v[i] != v[n-1])
        {
            cnt = i+1;
        }
    }
    if(cnt>=k)
        cout<<-1<<endl;
    else{
        cout<<cnt<<endl;
    }
    return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
        bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
                    return left.second < right.second;
                    }
};
int gcd (int a, int b) {
        return b ? gcd (b, a % b) : a;
}

int main()
{
    int n,k;
    SI(n);
    SI(k);
    vi v;
    int cnt = 0;
    bool flag = true;
    bool f3 = true;
    rep(i, n){
        int temp;
        SI(temp);
        v.PB(temp);
    }
    rep(i, v.size()){
        if(v[i]!=v[0])
        {
                f3 = false;
                break;
        }
    }
    if(f3){
        cout<<0<<endl;
        return 0;
    }
    vi v2 = v;
    vi v3;
    int o = 0;
    while(flag){
        v2.PB(v2[k-1]);
        v2.erase(v2.begin());
        if(v3 == v2){
            cout<<-1;
            return 0;
        }
        if(v != v2){
            cnt++;
        }
        else {
            cout<<-1<<endl;
            return 0;
        }
        bool f2 = true;

        rep(i, v2.size()){
            if(v2[i]!=v2[0])
            {
                f2 = false;
                break;
            }
        }
        if(f2){
            cout<<cnt<<endl;
            return 0;
        }
        if(o==10000){
            cout<<-1<<endl;
            return 0;
        }
        o++;

    }
    return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
	    bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
		            return left.second < right.second;
			        }
};
int gcd (int a, int b) {
		return b ? gcd (b, a % b) : a;
}

int main()
{
	int n,k;
	SI(n);
	SI(k);
	vi v;
	int cnt = 0;
	bool flag = true;
	bool f3 = true;
	rep(i, n){
		int temp;
		SI(temp);
		v.PB(temp);
	}
	rep(i, v.size()){
		if(v[i]!=v[0])
		{
				f3 = false;
				break;
		}
	}
	if(f3){
		cout<<0<<endl;
		return 0;
	}
	vi v2 = v;
	while(flag){
		v2.PB(v2[k-1]);
		v2.erase(v2.begin());
		if(v != v2){
			cnt++;
		}
		else {
			cout<<-1<<endl;
			return 0;
		}
		bool f2 = true;

		rep(i, v2.si


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
        bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
                    return left.second < right.second;
                    }
};
int gcd (int a, int b) {
        return b ? gcd (b, a % b) : a;
}

int main()
{
    int n,k;
    SI(n);
    SI(k);
    vi v;
    int cnt = 0;
    int cnt2= 0;
    bool flag = true;
    bool f3 = true;
    rep(i, n){
        int temp;
        SI(temp);
        v.PB(temp);
        if(i < k-1){
            cnt2++;
        }
    }
    bool f4 = false;
    repx(i, k-1, v.size()){
        if(v[i]!= v[k-1])
            f4 = true;
    }
    if(f4)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<k-1<<endl;
    return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
	    bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
		            return left.second < right.second;
			        }
};
int gcd (int a, int b) {
		return b ? gcd (b, a % b) : a;
}

int main()
{
	int n,k;
	SI(n);
	SI(k);
	vi v;
	int cnt = 0;
	bool flag = true;
	bool f3 = true;
	rep(i, n){
		int temp;
		SI(temp);
		v.PB(temp);
	}
	rep(i, v.size()){
		if(v[i]!=v[0])
		{
				f3 = false;
				break;
		}
	}
	if(f3){
		cout<<0<<endl;
		return 0;
	}
	vi v2 = v;
	vi v3;
	int o = 0;
	while(flag){
		v2.PB(v2[k-1]);
		v2.erase(v2.begin());
		if(v3 == v2){
			cout<<-1;
			return 0;
		}
		if(v != v2){
			cnt++;
		}
		else {
			cout<<-1<<endl;
			return 0;
		}
		bool f2 = true;

		rep(i, v2.size()){
			if(v2[i]!=v2[0])
			{
				f2 = false;
				break;
			}
		}
		if(f2){
			cout<<cnt<<endl;
			return 0;
		}
		if(o==1000000){
			cout<<-1<<endl;
			return 0;
		}
		o++;

	}
	return 0;
}


#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#define SI(x) scanf("%d", &x)
#define vi vector<int>
#define PB push_back
#define PI(x) printf("%d\n", x)
#define rep(i, n) for(int i=0;i<n;i++)
#define repx(i,x,n) for(int i=x;i<n;i++)
typedef long long LL;
typedef long long int LLI;
using namespace std;
struct sort_pred {
        bool operator()(const pair<int,int> &left, const pair<int,int> &right) {
                    return left.second < right.second;
                    }
};
int gcd (int a, int b) {
        return b ? gcd (b, a % b) : a;
}

int main()
{
    int n,k;
    SI(n);
    SI(k);
    vi v;
    int cnt = 0;
    int cnt2= 0;
    bool flag = true;
    bool f3 = true;
    rep(i, n){
        int temp;
        SI(temp);
        v.PB(temp);
        if(i < k-1){
            cnt2++;
        }
    }
    bool f4 = false;
    repx(i, k-1, v.size()){
        if(v[i]!= v[k-1])
            f4 = true;
    }
    if(f4)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<cnt2<<endl;
    return 0;
}


import java.util.Scanner;

public class B_Test {

	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			int N = sc.nextInt();

			long[] ns = new long[N];
			for (int i = 0; i < N; i++)
				ns[i] = sc.nextInt();

			int mod = 1;
			for (long K = N-1; K > 0; K--)
				for (int i = 0; i < K; i++) {
					ns[i] += ns[i + 1] * mod;
					mod *= -1;
				}

			System.out.println(remainder(ns[0]));
		}
	}

	static long remainder(long i) {
		final long b = (long) (Math.pow(10, 9) + 7);
		return (i >= 0) ? i % b : b + (i % b);
	}

}


import java.util.Scanner;

public class B_Test {
	final static long M = (long) 1e9 + 7;

	static long mul(long a, long b) {
		return a * b % M;
	}

	static long add(long a, long b) {
		return (((a + b) % M) + M) % M;
	}

	static long pow(long p, long e) {
		long r = 1;
		while (e > 0) {
			if (e % 2 == 1)
				r = mul(r, p);
			p = mul(p, p);
			e >>= 1;
		}
		return r;
	}

	public static void main(String[] args) {

		try (Scanner sc = new Scanner(System.in)) {

			int N = sc.nextInt();
			long[] as;

			as = new long[N];
			for (int i = 0; i < N; i++)
				as[i] = sc.nextLong();

			if (N == 1) {
				System.out.println(as[0]);
				return;
			}

			if (N % 2 == 1)
				for (int i = 0; i < N - 1; i++)
					as[i] = add(as[i], ((i % 2 == 0) ? 1 : -1) * as[i + 1]);

			N >>= 1;
			int sign = ((long) N * (N - 1) / 2 % 2 == 1) ? 1 : -1;

			long[] fac = new long[(int) N];
			long[] inv = new long[(int) N];
			fac[0] = 1;
			inv[0] = pow(1, M - 2);

			for (int i = 1; i < N; i++) {
				fac[i] = mul(i, fac[i - 1]);
				inv[i] = pow(fac[i], M - 2);
			}

			long res = 0;
			for (int i = 0; i < N; i++)
				res = add(res, mul(add(as[2 * i], sign * as[2 * i + 1]), mul(mul(fac[N - 1], inv[i]), inv[N - i - 1])));

			System.out.println(res);

		}
	}

}


package karen419;

import java.math.BigInteger;
import java.util.Scanner;

public class B_Test {

	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			int N = sc.nextInt();

			BigInteger[] ns = new BigInteger[N];
			for (int i = 0; i < N; i++)
				ns[i] = BigInteger.valueOf(sc.nextLong());

			BigInteger mod2 = BigInteger.valueOf(-1);
			BigInteger mod1 = BigInteger.ONE;
			BigInteger mod = mod1;

			for (long K = N-1; K > 0; K--)
				for (int i = 0; i < K; i++) {
					ns[i] = ns[i].add(ns[i + 1].multiply(mod));
					mod = (mod == mod1) ? mod2 : mod1;
				}

			System.out.println(ns[0].mod(BigInteger.valueOf((long)(Math.pow(10,9) + 7))));

		}
	}

}


import java.math.BigInteger;
import java.util.Scanner;

public class B_Test {

	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			int N = sc.nextInt();

			BigInteger[] ns = new BigInteger[N];
			for (int i = 0; i < N; i++)
				ns[i] = BigInteger.valueOf(sc.nextLong());

			BigInteger mod2 = BigInteger.valueOf(-1);
			BigInteger mod1 = BigInteger.ONE;
			BigInteger mod = mod1;

			for (long K = N - 1; K > 0; K--)
				for (int i = 0; i < K; i++) {
					ns[i] = ns[i].add(ns[i + 1].multiply(mod));
					mod = (mod == mod1) ? mod2 : mod1;
				}

			System.out.println(ns[0].mod(BigInteger.valueOf((long) (Math.pow(10, 9) + 7))));

		}
	}

}


import java.util.Scanner;

public class B_Test {

	final static long mod = (long) Math.pow(10, 9) + 7;

	public static void main(String[] args) {

		try (Scanner sc = new Scanner(System.in)) {

			int N = sc.nextInt();

			long[] ns = new long[N];
			for (int i = 0; i < N; i++)
				ns[i] = sc.nextLong();

			long mul = 1;
			for (long K = N - 1; K > 0; K--)
				for (int i = 0; i < K; i++) {
					ns[i] = remainder(ns[i] + remainder(ns[i + 1] * mul, mod), mod);
					mul *= -1;
				}

			System.out.println(remainder(ns[0], mod));
		}
	}

	final static long remainder(long n, long m) {
		return (n >= 0) ? n % m : m + (n % m);
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class B_Test {
	private static class Reader {
		static BufferedReader reader;
		static StringTokenizer tokenizer;

		static void init(InputStream input) {
			reader = new BufferedReader(new InputStreamReader(input));
			tokenizer = new StringTokenizer("");
		}

		static String next() throws IOException {
			while (!tokenizer.hasMoreTokens()) {
				// TODO add check for eof if necessary
				tokenizer = new StringTokenizer(reader.readLine());
			}
			return tokenizer.nextToken();
		}

		static long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		static int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		static double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
	}
	final static long M = (long) 1e9 + 7;

	static long mul(long a, long b) {
		return a * b % M;
	}

	static long add(long a, long b) {
		return (((a + b) % M) + M) % M;
	}

	static long pow(long p, long e) {
		long r = 1;
		while (e > 0) {
			if (e % 2 == 1)
				r = mul(r, p);
			p = mul(p, p);
			e >>= 1;
		}
		return r;
	}

	public static void main(String[] args) {

		try {
			Reader.init(System.in);

			int N = Reader.nextInt();
			long[] as;

			as = new long[N];
			for (int i = 0; i < N; i++)
				as[i] = Reader.nextLong();

			if (N == 1) {
				System.out.println(as[0]);
				return;
			}

			int sign = ((long) N * (N - 1) / 2 % 2 == 1) ? 1 : -1;

			if (N % 2 == 1) {
				for (int i = 0; i < N - 1; i++)
					as[i] = add(as[i], ((i % 2 == 0) ? 1 : -1) * as[i + 1]);
				N--;
			}

			long res = as[0];
			long ch = 1;
			for (int i = 1, k = 1; i < N; i++) {
				if(i % 2 == 0) {
					ch = mul(mul(ch,(N/2-k)), pow(k++,M-2));
					res = add(res,mul(as[i],ch));
				} else
					res = add(res,sign * mul(as[i],ch));
			}

			System.out.println(res);

		} catch (Exception e) {

		}
	}

}


import java.util.Arrays;
import java.util.Scanner;

public class B_Test {
	final static long M = (long) 1e9 + 7;

	static long mul(long a, long b) {
		return a * b % M;
	}

	static long add(long a, long b) {
		return (((a + b) % M) + M) % M;
	}

	static long choose(long n, long k) {
		return mul(fac[(int) n], mul(invs[(int) k], invs[(int) (n - k)]));
	}

	static long inv(long a) {
		return pow(a, M - 2);
	}

	static long pow(long p, long e) {
		long r = 1;
		while (e > 0) {
			if (e % 2 == 1)
				r = mul(r, p);
			p = mul(p, p);
			e >>= 1;
		}
		return r;
	}

	static long[] fac;
	static long[] invs;

	public static void main(String[] args) {

		try (Scanner sc = new Scanner(System.in)) {

			int N = sc.nextInt();

			final int last = (N > 1) ? (N * (N - 1) / 2 % 2 == 1) ? 1 : -1 : 1;

			long[] as = new long[N];
			for (int i = 0; i < N; i++)
				as[i] = sc.nextLong();

			fac = new long[N + 1];
			invs = new long[N + 1];

			fac[0] = 1;
			invs[0] = inv(1);

			for (int i = 1; i <= N; i++) {
				fac[i] = mul(fac[i - 1], i);
				invs[i] = inv(fac[i]);
			}

			long mul = 1;
			while (mul != 1 || N % 2 == 1) {
				for (int i = 1; i < N; i++) {
					as[i - 1] = add(as[i - 1], as[i] * mul);
					mul *= -1;
				}
				N--;
			}
			for (int i = 0, j = 0; j < N; i++, j += 2)
				as[i] = add(as[j], as[j + 1] * last);

			N = (N > 1) ? N >> 1 : 1;

			long res = 0;
			for (int i = 0; i < N; i++)
				res = add(res, mul(as[i], choose(N - 1, i)));
			System.out.println(res);
		}
	}

}


import java.util.Scanner;

public class B_Test {
	final static long M = (long) 1e9 + 7;

	static long mul(long a, long b) {
		return a * b % M;
	}

	static long add(long a, long b) {
		return (((a + b) % M) + M) % M;
	}

	static long pow(long p, long e) {
		long r = 1;
		while (e > 0) {
			if (e % 2 == 1)
				r = mul(r, p);
			p = mul(p, p);
			e >>= 1;
		}
		return r;
	}

	public static void main(String[] args) {

		try (Scanner sc = new Scanner(System.in)) {

			int N = sc.nextInt();
			long[] as;

			as = new long[N];
			for (int i = 0; i < N; i++)
				as[i] = sc.nextLong();

			if (N == 1) {
				System.out.println(as[0]);
				return;
			}

			if (N % 2 == 1)
				for (int i = 0; i < N - 1; i++)
					as[i] = add(as[i], ((i % 2 == 0) ? 1 : -1) * as[i + 1]);

			int sign = (N * (N - 1) / 2 % 2 == 1) ? 1 : -1;
			N >>= 1;

			long[] fac = new long[N];
			long[] inv = new long[N];
			fac[0] = 1;
			inv[0] = pow(1, M - 2);

			for (int i = 1; i < N; i++) {
				fac[i] = mul(i, fac[i - 1]);
				inv[i] = pow(fac[i], M - 2);
			}

			long res = 0;
			for (int i = 0; i < N; i++)
				res = add(res, mul(add(as[2 * i], sign * as[2 * i + 1]), mul(mul(fac[N - 1], inv[i]), inv[N - i - 1])));

			System.out.println(res);
		}
	}

}


import java.io.IOException;
import java.util.Scanner;

public class Main
{
    private static int MOD = 1000000007;

    public static void main(String[] args) throws IOException
    {
        Scanner in = new Scanner(System.in);
        int i, n, sgn, x, y, comb, ans;
        int[] a;

        n = in.nextInt();

        a = new int[n];
        for(i = 0; i < n; ++i) a[i] = in.nextInt();

        sgn = 1;
        while(n > 1 && (n % 2 == 1 || sgn == -1))
        {
            a = doSomething(a, sgn);
            if(n % 2 == 0) sgn = -sgn;
            --n;
        }

        if(n == 1)
        {
            System.out.println(a[0]);
            return;
        }

        n /= 2;

        x = 0; comb = 1;
        for(i = 0; i < n; ++i)
        {
            x = (int) (((long) x + (long) a[2 * i] * comb) % MOD);
            comb = ((int) ((long) comb * (n - i - 1)) % MOD);
            comb = (int) (((long) comb * (inv_mod(i + 1))) % MOD);
        }

        y = 0; comb = 1;
        for(i = 0; i < n; ++i)
        {
            y = (int) (((long) y + (long) a[2 * i + 1] * comb) % MOD);
            comb = ((int) ((long) comb * (n - i - 1)) % MOD);
            comb = (int) (((long) comb * (inv_mod(i + 1))) % MOD);
        }

        if( n % 2 == 1)
        {
            ans = x + y;
            if(ans >= MOD) ans -= MOD;
        }
        else
        {
            ans = x - y;
            if(ans < 0) ans += MOD;
        }

        System.out.println(ans);
    }

    private static int pow(int base, int exp)
    {
        int res;

        for(res = 1; exp > 0; exp >>= 1)
        {
            if(exp % 2 == 0) res = (int) (((long) res * base) % MOD);
            base = (int) (((long) base * base) % MOD);
        }

        return res;
    }

    private static int inv_mod(int i) {
        return pow(i, MOD - 2);
    }

    private static int[] doSomething(int[] a, int sgn)
    {
        int i;
        int[] b = new int[a.length - 1];

        for(i = 0; i < b.length; ++i)
        {
            b[i] = a[i] + sgn * a[i + 1];
            if(b[i] < 0) b[i] += MOD;
            if(b[i] >= MOD) b[i] -= MOD;
            sgn = -sgn;
        }

        return b;
    }
}


import java.io.IOException;
import java.util.Scanner;

public class Main
{
    private static int MOD = 1000000007;

    public static void main(String[] args) throws IOException
    {
        Scanner in = new Scanner(System.in);
        int i, n;
        long sgn, x, y, comb, ans;
        long[] a;

        n = in.nextInt();

        a = new long[n];
        for(i = 0; i < n; ++i) a[i] = in.nextInt();

        sgn = 1;
        while(n > 1 && (n % 2 == 1 || sgn == -1))
        {
            a = doSomething(a, sgn);
            if(n % 2 == 0) sgn = -sgn;
            --n;
        }

        if(n == 1)
        {
            System.out.println(a[0]);
            return;
        }

        n /= 2;

        x = 0; comb = 1;
        for(i = 0; i < n; ++i)
        {
            x = (x + a[2 * i] * comb) % MOD;
            comb = (comb * (n - i - 1)) % MOD;
            comb = (comb * (inv_mod(i + 1))) % MOD;
        }

        y = 0; comb = 1;
        for(i = 0; i < n; ++i)
        {
            y = (y + a[2 * i + 1] * comb) % MOD;
            comb = (comb * (n - i - 1)) % MOD;
            comb = (comb * (inv_mod(i + 1))) % MOD;
        }

        if( n % 2 == 1)
        {
            ans = x + y;
            if(ans >= MOD) ans -= MOD;
        }
        else
        {
            ans = x - y;
            if(ans < 0) ans += MOD;
        }

        System.out.println(ans);
    }

    private static long pow(long base, long exp)
    {
        long res;

        for(res = 1; exp > 0; exp >>= 1)
        {
            if(exp % 2 == 1) res = (res * base) % MOD;
            base = (base * base) % MOD;
        }

        return res;
    }

    private static long inv_mod(long i) {
        return pow(i, MOD - 2);
    }

    private static long[] doSomething(long[] a, long sgn)
    {
        int i;
        long[] b = new long[a.length - 1];

        for(i = 0; i < b.length; ++i)
        {
            b[i] = a[i] + sgn * a[i + 1];
            if(b[i] < 0) b[i] += MOD;
            if(b[i] >= MOD) b[i] -= MOD;
            sgn = -sgn;
        }

        return b;
    }
}


import java.io.IOException;
import java.util.Scanner;

public class Main
{
    private static int MOD = 1000000007;

    public static void main(String[] args) throws IOException
    {
        Scanner in = new Scanner(System.in);
        int i, n, sgn, x, y, comb, ans;
        int[] a;

        n = in.nextInt();

        a = new int[n];
        for(i = 0; i < n; ++i) a[i] = in.nextInt();

        sgn = 1;
        while(n > 1 && (n % 2 == 1 || sgn == -1))
        {
            a = doSomething(a, sgn);
            if(n % 2 == 0) sgn = -sgn;
            --n;
        }

        if(n == 1)
        {
            System.out.println(a[0]);
            return;
        }

        n /= 2;

        x = 0; comb = 1;
        for(i = 0; i < n; ++i)
        {
            x = (int) (((long) x + (long) a[2 * i] * comb) % MOD);
            comb = ((int) ((long) comb * (n - i - 1)) % MOD);
            comb = (int) (((long) comb * (inv_mod(i + 1))) % MOD);
        }

        y = 0; comb = 1;
        for(i = 0; i < n; ++i)
        {
            y = (int) (((long) y + (long) a[2 * i + 1] * comb) % MOD);
            comb = ((int) ((long) comb * (n - i - 1)) % MOD);
            comb = (int) (((long) comb * (inv_mod(i + 1))) % MOD);
        }

        if( n % 2 == 1)
        {
            ans = x + y;
            if(ans >= MOD) ans -= MOD;
        }
        else
        {
            ans = x - y;
            if(ans < 0) ans += MOD;
        }

        System.out.println(ans);
    }

    private static int pow(int base, int exp)
    {
        int res;

        for(res = 1; exp > 0; exp >>= 1)
        {
            if(exp % 2 == 1) res = (int) (((long) res * base) % MOD);
            base = (int) (((long) base * base) % MOD);
        }

        return res;
    }

    private static int inv_mod(int i) {
        return pow(i, MOD - 2);
    }

    private static int[] doSomething(int[] a, int sgn)
    {
        int i;
        int[] b = new int[a.length - 1];

        for(i = 0; i < b.length; ++i)
        {
            b[i] = a[i] + sgn * a[i + 1];
            if(b[i] < 0) b[i] += MOD;
            if(b[i] >= MOD) b[i] -= MOD;
            sgn = -sgn;
        }

        return b;
    }
}


import java.util.*;
import java.math.*;

public class c419B{

	public static int mod = 1000000007;

	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		long[] a = new long[n];
		for(int i=0; i<n; i++){
			a[i] = in.nextInt();
			if(a[i]<0)
				a[i]+=mod;
		}
		if(n%2==1){
			n--;
			for(int i=0; i<n; i++){
				if(i%2==0)
					a[i] = (a[i]+a[i+1])%mod;
				else
					a[i] = (a[i]-a[i+1]+mod)%mod;
			}
		}

		int num = n/2 -1;// the n in n choose k that we will use

		long result = 0;

		long c = 1; //num choose i = (num!)/(i!)(num-i)! = (num choose i-1)*(num-i+1)/i
		for(int i=0; i<=num; i++){
			if(i!=0){
				c*=num-i+1;
				c%=mod;
				c*=(new BigInteger(""+i)).modInverse(new BigInteger(""+mod)).longValue();
				c%=mod;
			}
			result += c*a[2*i];
			result -= (c*a[(2*i)+1])%mod;
			result += mod;
			result %= mod;
		}
		System.out.println(result);
	}

}


import java.util.*;
import java.math.*;

public class c419B{

	public static int mod = 1000000007;

	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		long[] a = new long[n];
		for(int i=0; i<n; i++){
			a[i] = in.nextInt();
			if(a[i]<0)
				a[i]+=mod;
		}
		if(n==1){
			System.out.println(a[0]);
			return;
		}
		if(n%2==1){
			n--;
			for(int i=0; i<n; i++){
				if(i%2==0)
					a[i] = (a[i]+a[i+1])%mod;
				else
					a[i] = (a[i]-a[i+1]+mod)%mod;
			}
		}

		int num = n/2 -1;// the n in n choose k that we will use

		long result = 0;

		long c = 1; //num choose i = (num!)/(i!)(num-i)! = (num choose i-1)*(num-i+1)/i
		for(int i=0; i<=num; i++){
			if(i!=0){
				c*=num-i+1;
				c%=mod;
				c*=(new BigInteger(""+i)).modInverse(new BigInteger(""+mod)).longValue();
				c%=mod;
			}
			result += c*a[2*i];
			result -= (c*a[(2*i)+1])%mod;
			result += mod;
			result %= mod;
		}
		System.out.println(result);
	}

}


import java.util.*;
import java.math.*;

public class c419B{

	public static int mod = 1000000007;

	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		long[] a = new long[n];
		for(int i=0; i<n; i++){
			a[i] = in.nextInt();
			if(a[i]<0)
				a[i]+=mod;
		}
		if(n==1){
			System.out.println(a[0]);
			return;
		}
		if(n%2==1){
			n--;
			for(int i=0; i<n; i++){
				if(i%2==0)
					a[i] = (a[i]+a[i+1])%mod;
				else
					a[i] = (a[i]-a[i+1]+mod)%mod;
			}
		}

		int num = n/2 -1;// the n in n choose k that we will use

		long result = 0;

		long c = 1; //num choose i = (num!)/(i!)(num-i)! = (num choose i-1)*(num-i+1)/i
		for(int i=0; i<=num; i++){
			if(i!=0){
				c*=num-i+1;
				c%=mod;
				c*=(new BigInteger(""+i)).modInverse(new BigInteger(""+mod)).longValue();
				c%=mod;
			}
			result += c*a[2*i];
			if(n%4==0)
				result -= (c*a[(2*i)+1])%mod;
			else
				result += (c*a[(2*i)+1])%mod;
			result += mod;
			result %= mod;
		}
		System.out.println(result);
	}

}


//package round419;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class B2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int n = ni();
		int[] a = na(n);
		int mod = 1000000007;
		int[][] fif = enumFIF(400005, mod);
		long ret = 0;
		if(n % 4 == 1){
			for(int i = 0;i < n;i+=2){
				ret += a[i] * C(n/2, i/2, mod, fif);
				ret %= mod;
			}
		}else if(n % 4 == 2){
			for(int i = 0;i < n;i+=2){
				ret += (a[i] + a[i+1]) * C((n-1)/2, i/2, mod, fif);
				ret %= mod;
			}
		}else if(n % 4 == 3){
			for(int i = 0;i < n-2;i+=2){
				ret += ((long)a[i] + 2*a[i+1] - a[i+2]) * C((n-2)/2, i/2, mod, fif);
				ret %= mod;
			}
		}else{
			for(int i = 0;i < n;i+=2){
				ret += (a[i] - a[i+1]) * C(n/2-1, i/2, mod, fif);
				ret %= mod;
			}
		}
		if(ret < 0)ret += mod;
		out.println(ret);
	}

	public static long C(int n, int r, int mod, int[][] fif) {
		if (n < 0 || r < 0 || r > n)
			return 0;
		return (long) fif[0][n] * fif[1][r] % mod * fif[1][n - r] % mod;
	}

	public static int[][] enumFIF(int n, int mod) {
		int[] f = new int[n + 1];
		int[] invf = new int[n + 1];
		f[0] = 1;
		for (int i = 1; i <= n; i++) {
			f[i] = (int) ((long) f[i - 1] * i % mod);
		}
		long a = f[n];
		long b = mod;
		long p = 1, q = 0;
		while (b > 0) {
			long c = a / b;
			long d;
			d = a;
			a = b;
			b = d % b;
			d = p;
			p = q;
			q = d - c * q;
		}
		invf[n] = (int) (p < 0 ? p + mod : p);
		for (int i = n - 1; i >= 0; i--) {
			invf[i] = (int) ((long) invf[i + 1] * (i + 1) % mod);
		}
		return new int[][] { f, invf };
	}

	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

	public static void main(String[] args) throws Exception { new B2().run(); }

	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }

	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}

	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}

	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public int mod = 1000000007;
        public long[] fact;
        public long[] ifact;

        public long comb(int n, int k) {
            if (k < 0 || k > n) return 0;
            return fact[n] * ifact[k] % mod * ifact[n - k] % mod;
        }

        public int solveSmart(int[] arr) {
            int n = arr.length;
            long[][] x = Factorials.getFIF(n + n + 1010, mod);
            fact = x[0];
            ifact = x[1];
            if (n % 4 == 0) {
                int b = n / 2 - 1;
                long ret = 0;
                for (int i = 0; i < arr.length; i++) {
                    long mult = comb(b, i / 2);
                    if (i % 2 == 1) mult = mod - mult;
                    ret = (mult * arr[i] + ret) % mod;
                }
                return (int) ret;
            } else if (n % 4 == 1) {
                int b = (n - 1) / 2;
                long ret = 0;
                for (int i = 0; i < arr.length; i += 2) {
                    long mult = comb(b, i / 2);
                    ret = (mult * arr[i] + ret) % mod;
                }
                return (int) ret;
            } else if (n % 4 == 2) {
                int b = (n - 2) / 2;
                long ret = 0;
                for (int i = 0; i < arr.length; i++) {
                    long mult = comb(b, i / 2);
                    ret = (mult * arr[i] + ret) % mod;
                }
                return (int) ret;
            } else if (n % 4 == 3) {
                int b = (n - 3) / 2;
                long ret = 0;
                for (int i = 0; i < arr.length; i++) {
                    long mult = comb(b, i / 2);
                    if (i % 2 == 1) mult = mult * 2 % mod;
                    else {
                        if (i * 2 >= arr.length) {
                            int t = arr.length - 1 - i;
                            mult = comb(b, t / 2);
                            mult = mult * (b - t + 1) % mod * x[2][b - t / 2 + 1] % mod;
                            mult = mod - mult;
                        } else {
                            mult = mult * (b - i + 1) % mod * x[2][b - i / 2 + 1] % mod;
                        }
                    }
                    ret = (mult * arr[i] + ret) % mod;
                }
                return (int) ret;
            }
            return 0;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int[] arr = in.readIntArray(n);
            out.println(solveSmart(arr));
        }

    }

    static class Factorials {
        public static long[][] getFIF(int max, int mod) {
            long[] fact = new long[max];
            long[] ifact = new long[max];
            long[] inv = new long[max];
            inv[1] = 1;
            for (int i = 2; i < max; i++) {
                inv[i] = (mod - mod / i) * inv[mod % i] % mod;
            }
            fact[0] = 1;
            ifact[0] = 1;
            for (int i = 1; i < max; i++) {
                fact[i] = fact[i - 1] * i % mod;
                ifact[i] = ifact[i - 1] * inv[i] % mod;
            }
            return new long[][]{fact, ifact, inv};
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int[] readIntArray(int tokens) {
            int[] ret = new int[tokens];
            for (int i = 0; i < tokens; i++) {
                ret[i] = nextInt();
            }
            return ret;
        }

        public int read() {
            if (this.numChars == -1) {
                throw new InputMismatchException();
            } else {
                if (this.curChar >= this.numChars) {
                    this.curChar = 0;

                    try {
                        this.numChars = this.stream.read(this.buf);
                    } catch (IOException var2) {
                        throw new InputMismatchException();
                    }

                    if (this.numChars <= 0) {
                        return -1;
                    }
                }

                return this.buf[this.curChar++];
            }
        }

        public int nextInt() {
            int c;
            for (c = this.read(); isSpaceChar(c); c = this.read()) {
                ;
            }

            byte sgn = 1;
            if (c == 45) {
                sgn = -1;
                c = this.read();
            }

            int res = 0;

            while (c >= 48 && c <= 57) {
                res *= 10;
                res += c - 48;
                c = this.read();
                if (isSpaceChar(c)) {
                    return res * sgn;
                }
            }

            throw new InputMismatchException();
        }

        public static boolean isSpaceChar(int c) {
            return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
        }

    }
}