#include <bits/stdc++.h>
#define M 1000000007
#define fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define d(x) cout << #x << " " << x << "\n";
#define min(x1, x2) (x1 > x2 ? x2 : x1)
#define max(x1, x2) (x1 < x2 ? x2 : x1)
#define min3(x1, x2, x3) (x3 > min(x1, x2) ? min(x2, x1) : x3)
#define max3(x1, x2, x3) (x3 < max(x1, x2) ? max(x1, x2) : x3)
#define ll long long int
#define ul unsigned long long int
#define p pair<ll, ll>
#define ld long double
#define dv(v)                            \
    cerr << #v << "  ";                  \
    for (int i = 0; i < (v).size(); i++) \
        cerr << v[i] << " ";             \
    cerr << "\n";
#define inf INT_MAX
#define mp(x, y) make_pair(x, y)
using namespace std;

vector<tuple<ul, ul, ul>> v;
ul dp[1 << 18][101];
int n;
int m;

ul fact[19];
void fac(ul n)
{
    fact[0] = 1;
    for (ul i = 1; i <= n; ++i)
    {
        fact[i] = (fact[i - 1] * i);
    }
}

ll rec(ul i, int j)
{
    if (dp[i][j] != -1)
    {
        return dp[i][j];
    }
    bitset<18> b(i);
    if (j == m)
    {
        int tn = 0;
        for (int k = 0; k < n; ++k)
        {
            if (b[k] == 1)
            {
                ++tn;
            }
        }
        return dp[i][j] = fact[n - tn];
    }
    ul x = get<0>(v[j]);
    ul y = get<1>(v[j]);
    ul z = get<2>(v[j]);
    int no = 0;
    int tn = 0;
    for (int k = 0; k < n; ++k)
    {
        if (b[k] == 1)
        {
            ++tn;
        }
        if (b[k] == 1 && k + 1 <= y)
        {
            ++no;
        }
    }
    if (no > z)
    {
        return dp[i][j] = 0;
    }
    else
    {
        if (tn == x)
        {
            return dp[i][j] = rec(i, j + 1);
        }
        else
        {
            dp[i][j] = 0;
            for (ll k = 0; k < n; ++k)
            {
                if (b[k] == 0)
                {
                    dp[i][j] += rec(i + (1 << k), j);
                }
            }
            return dp[i][j];
        }
    }
}

int main()
{
    fio;
    ll t, x, y, z, k;
    cin >> n >> m;
    memset(dp, -1, sizeof(dp));
    fac(18);
    t = m;
    while (t--)
    {
        cin >> x >> y >> z;
        v.push_back(make_tuple(x, y, z));
    }
    sort(v.begin(), v.end());
    cout << rec(0, 0) << "\n";
    return 0;
}
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1 << 18;
int n, a[N], b[N], p[N], inv[N], ord[N];
vector<pair<int, int>> op;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

    int n; cin >> n;
    rep(i, 0, n) cin >> a[i];
    rep(i, 0, n) cin >> b[i];
    rep(j, 0, n) cin >> p[j], --p[j], inv[p[j]] = j;

    bool impossible = false;
    rep(i, 0, n) if (p[i] != i && a[i] <= b[p[i]]) impossible = true;
    if (impossible) return cout << "-1", 0;
    
    rep(i, 0, n) ord[i] = i;
    sort(ord, ord + n, [&](int x, int y) { return a[x] < a[y]; });

    rep(k, 0, n) {
        int i = ord[k];
        if (p[i] == i) continue;
        int j = inv[i];
        op.emplace_back(i, j);
        inv[i] = i;
        p[j] = p[i];
        inv[p[j]] = j;
    }
    cout << sz(op) << '\n';
    for (auto [i, j] : op) cout << i + 1 << ' ' << j + 1 << '\n';
}