#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
int n,m,X[100010],Y[100010],c[110];
vector<int> g[110];
int ans[110][110];
bool vis[110];
int st[110],tot,inst[110];
void dfs(int u,int p) {
	//printf("%d\n",u);
	vis[u]=1; st[++tot]=u; inst[u]=1;
	for (int i=0,v;i<g[u].size();i++) {
		v=g[u][i];
		if (v==p) continue;
		if (!vis[v]) {
			dfs(v,u);
			ans[u][v]=1,ans[v][u]=0;
		} else if (inst[v]) {
			ans[u][v]=1,ans[v][u]=0;
		}
	}
	inst[u]=0,tot--;
}
vector<int> G[110];
int t[110],cnt;
void ddfs(int u) {
	if (t[u]) return;
	t[u]=1,cnt++;
	for (int i=0,v;i<G[u].size();i++) {
		v=G[u][i];
		ddfs(v);
	}
}
int main() {
	//freopen("1.txt","r",stdin);
	read(n),read(m); int x,y;
	for (int i=1;i<=m;i++) {
		read(x),read(y);
		X[i]=x,Y[i]=y;
	}
	memset(ans,-1,sizeof(ans));
	for (int i=1;i<=n;i++) read(c[i]);
	for (int i=1;i<=m;i++) {
		x=X[i],y=Y[i];
		if (c[x]==c[y]) {
			g[x].push_back(y),g[y].push_back(x);
			//printf("add %d %d\n",x,y);
		} else {
			if (c[x]>c[y]) ans[x][y]=1,ans[y][x]=0; else ans[y][x]=1,ans[x][y]=0;
		}
	}
	for (int i=1;i<=n;i++) if (!vis[i]) {
		dfs(i,0);
	}
	for (int i=1;i<=m;i++) {
		x=X[i],y=Y[i];
		if (ans[x][y]) puts("->");
		else puts("<-");
		
		//if (ans[x][y]) G[x].push_back(y);//,printf("%d %d\n",x,y);
		//else G[y].push_back(x);//,printf("%d %d\n",y,x);
	}
	/*
	for (int i=1;i<=n;i++) {
		for (int j=1;j<=n;j++) t[j]=0; cnt=0;
		ddfs(i);
		if (cnt!=c[i]) puts("WA");
		//printf("%d ",cnt);
	}
	//puts("");
	puts("AC");*/
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?
  
  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?
    
  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?
    
  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

vector<int> solve(const vector<int>& A, const vector<int>& B){
    const int N = A.size();
    vector<int> res(N, -1);
    int last = -1;
    for(int i=0;i<N;i++){
        if(A[i] == B[i]){
            res[i] = 0;
            last = i;
        } else if(A[i] < B[i]){
            last = -1;
        } else if(last != -1){
            int L = last, R = i;
            while(R-L > 1){
                int mid = (L+R)/2;
                int dst = A[mid] + i - mid;
                if(dst <= B[i]) L = mid;
                else R = mid;
            }
            if(A[L]+i-L == B[i]){
                res[i] = i-L;
            }
        }
    }
    for(int i=N-1;i>0;i--){
        if(res[i] > 0 && res[i-1] < res[i]) res[i] = 1;
    }
    return res;
}

int main(){
    int N, L; cin >> N >> L;
    vector<int> A(N+2, 0), B(N+2, 0);
    for(int i=1;i<=N;i++) cin >> A[i];
    for(int i=1;i<=N;i++) cin >> B[i];
    A.back() = L+1;
    B.back() = L+1;
    vector<int> res(N+2, -1);
    auto v = solve(A, B);
    for(int i=0;i<N+2;i++) if(v[i] != -1) res[i] = v[i];
    reverse(A.begin(), A.end());
    reverse(B.begin(), B.end());
    for(auto& t : A) t = L+1-t;
    for(auto& t : B) t = L+1-t;
    v = solve(A, B);
    for(int i=0;i<N+2;i++) if(v[i] != -1) res[N+1-i] = v[i];
    if(all_of(res.begin(), res.end(), [](int t){ return t != -1; })){
        cout << accumulate(res.begin(), res.end(), 0LL) << endl;
    } else {
        cout << -1 << endl;
    }
}