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

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << "Line " << __LINE__ << ": " <<  #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;

template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;
struct edge{
    ll to, idx;
};

using Graph = V<V<edge>>;

int main(){

    ll N, M;
    cin >> N >> M;
    Graph G(N);
    V<ll> a(M), b(M);
    REP(i, M) cin >> a[i] >> b[i], a[i]--, b[i]--;
    REP(i, M){
        G[a[i]].push_back(edge{b[i], i});
        G[b[i]].push_back(edge{a[i], i});
    }
    V<ll> c(N);
    REP(i, N) cin >> c[i];
    V<ll> res(M, -1);

    REP(i, M){
        if(res[i] == -1){
            if(c[a[i]] > c[b[i]]){
                res[i] = 1;
            }else if(c[a[i]] < c[b[i]]){
                res[i] = 0;
            }
        }
    }

    V<ll> ids(N, -1);
    ll cnt = 0;
    auto dfs = [&](auto self, ll now, ll par)->void{
        if(ids[now] != -1) return;
        ids[now] = cnt;
        cnt++;
        for(auto &e: G[now]){
            if(e.to == par) continue;
            if(res[e.idx] != -1) continue;

            if(ids[e.to] == -1){
                if(now == a[e.idx]) res[e.idx] = 1;
                else res[e.idx] = 0;
                self(self, e.to, now);
            }else{
                dump(now)
                if(now == a[e.idx]) res[e.idx] = 1;
                else res[e.idx] = 0;
            }
        }
        return;
    };

    REP(i, N) dfs(dfs, i, -1);
    // dump(ids)

    REP(i, M){
        assert(res[i] != -1);
        cout << (res[i] == 0 ? "<-" : "->") << endl;
    }


    return 0;
}
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <complex>
#include <vector>
#include <limits>
#include <iomanip>
#include <cassert>
#include <numeric>
#include <chrono>
#include <random>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for(int i = 0;i < n;i++)
#define repr(i, n) for(int i = n-1;i >= 0;i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second

const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;

int N, L;
int A[1<<20], B[1<<20];
vector<int> dA, dB;

void solve() {
  cin>>N>>L;
  rep(i,N)cin>>A[i];
  rep(i,N)cin>>B[i];
  dA.pb(A[0]-1);
  dB.pb(B[0]-1);
  for(int i = 1;i < N;i++) {
    dA.pb(A[i]-A[i-1]-1);
    dB.pb(B[i]-B[i-1]-1);
  }
  dA.pb(L-A[N-1]);
  dB.pb(L-B[N-1]);

  // for(auto x:dA)cout<<x<<" ";cout<<endl;
  // for(auto x:dB)cout<<x<<" ";cout<<endl;

  int sj = 0;ll ops = 0;
  for(int i = 0;i < dB.size();i++) {
    if(dB[i] == 0) continue;
    while(sj < dA.size() && dA[sj] == 0) sj++;

    int sum = 0, ej = sj;
    while (ej < dA.size() && sum < dB[i]) {
      sum += dA[ej++];
    }

    if(sum != dB[i]) {
      cout << -1 << endl;
      return;
    }

    int a = sj, b = ej-1, c = i;
    ops += max(b,c)-min(a,c);
    sj = ej;
  }

  cout << ops << endl;

}

int main() {

 // freopen("input.in","r",stdin);
 // freopen("output.out","w",stdout);
 ios_base::sync_with_stdio(false);
 cin.tie(nullptr);
 int t=1;//cin>>t;
 while (t--) {
   solve();
 }

 return 0;
}
