#include <bits/stdc++.h>

using namespace std;

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldbl;
typedef pair<int, int> pii;
typedef pair<uint, uint> puu;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<uint> vu;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pii> vpii;
typedef vector<puu> vpuu;
typedef vector<pll> vpll;
typedef vector<pull> vpull;
typedef vector<string> vstr;
typedef vector<double> vdbl;
typedef vector<ldbl> vldbl;
#define pb push_back
#define ppb pop_back
#define pfr push_front
#define ppfr pop_front
#define emp emplace
#define empb emplace_back
#define be begin
#define rbe rbegin
#define all(x) (x).be(), (x).end()
#define rall(x) (x).rbe(), (x).rend()
#define fir first
#define sec second
#define mkp make_pair
#define brif(cond) if (cond) break
#define ctif(cond) if (cond) continue
#define retif(cond) if (cond) return
void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;}
template<typename T> T extgcd(T a, T b, T &x, T &y)
{
    T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
    while (b) {
        T q = a/b; a %= b; swap(a, b);
        x0 -= q*x1; swap(x0, x1);
        y0 -= q*y1; swap(y0, y1);
    }
    x = x0; y = y0; return a;
}
int ctz(uint x) {return __builtin_ctz(x);}
int ctzll(ull x) {return __builtin_ctzll(x);}
int clz(uint x) {return __builtin_clz(x);}
int clzll(ull x) {return __builtin_clzll(x);}
int popcnt(uint x) {return __builtin_popcount(x);}
int popcntll(ull x) {return __builtin_popcountll(x);}
int bsr(uint x) {return 31^clz(x);}
int bsrll(ull x) {return 63^clzll(x);}

#define N 100032

char dir[N];
int a[N], b[N];

ll calc(int i, int j, int e)
{
    //cerr << "calc, e = " << e << '\n';
    //cerr << "  a = ["; for (int k = i; k < j; ++k) cerr << ' ' << a[k]; cerr << " ]\n";
    //cerr << "  b = ["; for (int k = i; k < j; ++k) cerr << ' ' << b[k]; cerr << " ]\n";
    ll res = 0;
    set<int> x;
    for (int k = i; k < j; ++k) {
        x.erase(a[k]);
        x.insert(b[k]);
        res += x.size();
    }
    if (x.size() > 1 || b[j-1] != e) res = -1;
    //cerr << "  res = " << res << '\n';
    return res;
}

void flip(int x[], int i, int j)
{
    reverse(x+i, x+j);
    for (int k = i; k < j; ++k) x[k] = -x[k];
}

int main()
{
    canhazfast();

    int n, w;
    ll ans = 0;

    cin >> n >> w;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        a[i] -= i;
    }
    for (int i = 1; i <= n; ++i) {
        cin >> b[i];
        b[i] -= i;
    }
    //cerr << "shifted:\n";
    //cerr << "a = ["; for (int i = 1; i <= n; ++i) cerr << ' ' << a[i]; cerr << " ]\n";
    //cerr << "b = ["; for (int i = 1; i <= n; ++i) cerr << ' ' << b[i]; cerr << " ]\n";

    a[n+1] = b[n+1] = w-n;
    for (int i = 1; i <= n; ++i) {
        dir[i] = '-';
        if (a[i] < b[i]) dir[i] = 'R';
        if (a[i] > b[i]) dir[i] = 'L';
    }
    //cerr << "dir = " << dir+1 << '\n';
    for (int i = 1; i < n; ++i) {
        retif(dir[i] == 'R' && dir[i+1] == 'L') cout << "-1\n", 0;
    }
    for (int i = 1, j = 1; i <= n; i = j) {
        while (j <= n && dir[j] == dir[i]) ++j;
        ctif(dir[i] == '-');
        int e = a[j];
        if (dir[i] == 'L') {
            flip(a, i, j);
            flip(b, i, j);
            e = -a[i-1];
        }
        ll res = calc(i, j, e);
        retif(res < 0) cout << "-1\n", 0;
        ans += res;
    }
    cout << ans << '\n';

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<string> vs;
typedef vector<double> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vp> vvp;

// input
int N, Q;
vl A;


void input() {
    cin >> N >> Q;
    A = vl(N);
    rep(i, N) cin >> A[i];
}


struct SegmentTree {
    int n;
    vector<int> left, right;
    vector<ll> dat;

    SegmentTree(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        left = right = vector<int>(2 * n - 1);
        dat = vector<ll>(2 * n - 1, 0);
        set_range(0, 0, n);
    }

    void set_range(int i, int l, int r) {
        left[i] = l;
        right[i] = r;
        if (i < n - 1) {
            set_range(2 * i + 1, l, (l + r) / 2);
            set_range(2 * i + 2, (l + r) / 2, r);
        }
    }

    void add(int i, ll x) {
        i += n - 1;
        dat[i] ^= x;
        while (i > 0) {
            i = (i - 1) / 2;
            dat[i] ^= x;
        }
    }

    ll sum_recur(int i, int l, int r) {
        if (left[i] == l && right[i] == r) return dat[i];
        int c = (left[i] + right[i]) / 2;
        ll ret = 0;
        if (l < c) ret ^= sum_recur(2 * i + 1, l, min(c, r));
        if (c < r) ret ^= sum_recur(2 * i + 2, max(c, l), r);
        return ret;
    }

    ll sum(int l, int r) {
        return sum_recur(0, l, r);
    }
};


int main() {
    input();
    SegmentTree st(N);
    rep(i, N) st.add(i, A[i]);
    rep(i, Q) {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1) {
            st.add(x - 1, y);
        } else {
            ll ans = st.sum(x - 1, y);
            cout << ans << endl;
        }
    }
}
