/**
 *    author:  otera    
**/
#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60;
#define rep(i, n) for(int i = 0; i < n; ++ i)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
    int n, m; cin >> n >> m;
    vector<int> c(n);
    vector<vector<int>> g(n, vector<int>());
    vector<int> a(m), b(m);
    vector<int> ans(m, -1);
    map<P, P> mp;
    rep(i, m) {
        cin >> a[i] >> b[i]; -- a[i], -- b[i];
        g[a[i]].push_back(b[i]);
        g[b[i]].push_back(a[i]);
        mp[{a[i], b[i]}] = {i, 1}, mp[{b[i], a[i]}] = {i, 0};
    }
    rep(i, n) {
        cin >> c[i];
    }
    vector<bool> used(m, 0);
    rep(i, m) {
        if(c[a[i]] < c[b[i]]) {
            // <-
            ans[i] = 0;
            used[i] = 1;
        } else if(c[a[i]] > c[b[i]]) {
            // ->
            ans[i] = 1;
            used[i] = 1;
        }
    }
    auto rec = [&](auto && self, int p, int v) -> void {
        for(int nv: g[v]) {
            if(nv == p) continue;
            if(c[v] == c[nv]) {
                // v -> nv
                if(mp.count({v, nv})) {
                    P p = mp[{v, nv}];
                    int i = p.fr, val = p.sc;
                    if(used[i]) continue;
                    used[i] = 1;
                    ans[i] = val;
                    self(self, v, nv);
                }
            }
        }
    };
    rep(i, m) {
        if(!used[i]) {
            assert(c[a[i]] == c[b[i]]);
            used[i] = 1;
            ans[i] = 1;
            rec(rec, a[i], b[i]);
        }
    }
    rep(i, m) {
        assert(ans[i] != -1);
        if(ans[i] == 1) cout << "->" << "\n";
        else cout << "<-" << "\n";
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(20);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define getchar_unlocked getchar

int A[100005];
int B[100005];
int offset[100005];
int minLevel1[100005];
int minLevel2[100005];
int N, L;

inline int nextInt(){
    int x=0;
    char ch=0;
    do{
        ch=getchar_unlocked();
    }while(ch<'0'||ch>'9');
    while(ch>='0'&&ch<='9'){
        x=10*x+(ch-'0');
        ch=getchar_unlocked();
    }
    return x;
}

int main(){
    N = nextInt();
    L = nextInt();
    for(int i = 1; i <= N; i ++){
        A[i] = nextInt();
        offset[i] = A[i]-i;
    }
    for(int i = 1; i <= N; i ++){
        B[i] = nextInt();
    }

    for(int i = 1; i <= N; i ++){
        minLevel1[i] = 0;
        minLevel2[i] = 0;
        //printf("offset[%d]=%d\n", i, offset[i]);
    }

    long long ans = 0;
    int lo = 1;
    int hi2 = 1;
    for(int i = 1; i <= N; i ++){
        if(A[i] == B[i]){continue;}
        if(B[i] == i || B[i] == L-N+i){
            ans ++;
        }else{
            while(offset[lo] < B[i]-i && lo < N+2){
                lo ++;
            }

            if((lo == N+2) || (offset[lo] != B[i]-i)){
                printf("-1");
                return 0;
            }

            //hi2 = max(hi2, lo);
            while(offset[hi2] <= B[i]-i && hi2 < N+2){
                hi2 ++;
            }

            int hi = hi2-1;
            //int hi = upper_bound(offset+1, offset+(1+N), B[i]-i) - offset - 1;

            if(lo > i){
                minLevel2[lo] = max(minLevel2[lo], lo - i);
            }else{
                minLevel1[hi] = max(minLevel1[hi], i - hi);
            }

            //printf("i=%d [%d, %d]\n", i, lo, hi);
            //ans += min(abs(lo - i), abs(hi - i));
        }
    }

    for(int i = 1; i <= N; i ++){
        ans += minLevel1[i] + minLevel2[i];
    }

    printf("%lld\n", ans);
    return 0;
}
