#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
template <class T> T read(T &a) {
    a=0;char x=getchar();bool f=0;
    for(;x<'0'||x>'9';x=getchar())f|=x=='-';
	for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0';
    if(f)a=-a;
    return a;
}
using namespace std;

const int N = 1e5 + 5;

int a[N], b[N], n, m;

int main() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) {
        read(a[i]);
    }
    for (int i = 1; i <= n; ++i) {
        read(b[i]);
    }
    n++;
    a[0] = b[0] = 0;
    a[n] = b[n] = m + 1;
    for (int i = n; i >= 1; --i) {
        a[i] = a[i] - a[i - 1] - 1;
        b[i] = b[i] - b[i - 1] - 1;
    }
    LL ans = 0;
    for (int i = 1, j = 1; i <= n; ++i) {
        if (!b[i]) continue;
        while (!a[j]) j++;
        int k = j, sum = 0;
        while (sum < b[i] && j <= n) {
            sum += a[j++];
        }
        if (sum != b[i]) {
            puts("-1");
            return 0;
        }
        ans += max(i - k, 0) + max(j - i - 1, 0);
    }
    printf("%lld\n", ans);
	return 0;
}

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

int n,k;
char dp[5005][5005],a[1000005];

int ddd[1000]={0};

char win(char a,char b){
    if(a==b)  return a;
    if(a=='S'&&b=='P')
        return a;
    if(a=='P'&&b=='S')
        return b;

    if(ddd[a]>ddd[b])
        return a;

    else return b;
}

int getmod(int ppp,int n){
    if(ppp%n==0)
        return n;
    else
        return ppp%n;
}

int quick_power(int x,int y){
    int res=1;
    for(;y;y=y>>1,x=(x*x)%n)
        if(y&1)
            res=(res*x)%n;

    return res;
}

signed main(){
    ios::sync_with_stdio(false);

    cin>>n>>k;
    ddd['P']=2,ddd['R']=1,ddd['S']=0;

    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=n+1;i<=2*n;i++)
        a[i]=a[i-n];
    for(int i=1;i<=2*n;i++)
        dp[i][0]=a[i];

    for(int i=1;i<=k;i++){
        for(int j=1;j<=n;j++)
            dp[j][i]=win(dp[j][i-1],dp[getmod(j+quick_power(2,i-1),n)][i-1]);
        for (int j=n+1;j<=2*n;j++)
            dp[j][i]=dp[j-n][i];
    }

    cout<<dp[1][k]<<endl;
    return 0;
}