/*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
using vll = vector<ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
ostream& operator<<(ostream& os, lpair& h){ os << "(" << h.first << ", " << h.second << ")"; return os;}
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
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 (b<a) { a=b; return 1; } return 0; }

void solve(){
    ll N;
    cin >> N;
    vll a(N), b(N), c(N), d(N), e(N);
    vector<vll> v(N, vll(5));
    rep(i,0,N){
        rep(j,0,5){
            cin >> v[i][j];
        }
    }
    ll ans = 0;
    vector<vector<lpair> > alt(1LL<<5);
    rep(i,0,N){
        rep(bit,1,(1LL<<5)){
            ll min_val = INF;
            rep(j,0,5){
                if(bit & (1LL<<j)){
                    chmin(min_val, v[i][j]);
                }
            }
            alt[bit].push_back({min_val, i});
        }
    }
    rep(i,0,1LL<<5){
        sort(alt[i].begin(), alt[i].end(), greater<lpair>());
    }

    rep(i,0,N){
        rep(j,i+1,N){
            ll lv = 0, rv = 1e9+7;
            while(rv - lv > 1){
                ll mid = (rv + lv) / 2;
                ll bit = 0;
                rep(k,0,5){
                    if(max(v[i][k], v[j][k]) < mid){
                        bit += (1LL<<k);
                    }
                }
                if(bit == 0){
                    lv = mid;
                    continue;
                }
                bool ok = false;
                ll n = min(3LL, (ll)alt[bit].size());
                rep(k,0,n){
                    if(alt[bit][k].first >= mid && alt[bit][k].second != i && alt[bit][k].second != j){
                        ok = true;
                    }
                }
                if(ok){
                    lv = mid;
                }else{
                    rv = mid;
                }
            }
            // print(i,j,lv);
            chmax(ans, lv);
        }
    }
    print(ans);
    
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("unroll-loops, no-stack-protector")
#pragma GCC target ("avx")
#pragma GCC target ("avx2")
#pragma GCC target ("fma")
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI acos(-1)
#define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update>
#pragma 03
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// 1 2 3 4 2 3 1
bool vis[200005], strong[200005];
ll p[200005], a[200005], b[200005];
int main(){
	fastio;
	ll n; cin >> n;
	for (ll i = 0; i < n; i++) cin >> a[i];
	for (ll i = 0; i < n; i++) cin >> b[i];
	for (ll i = 0; i < n; i++) cin >> p[i], p[i]--;
	ll ans = 0, cyc_len = 0;
	for (ll i = 0; i < n; i++){
		if (p[i] == i) continue;
		if (a[i] <= b[p[i]]){
			cout << -1 << "\n"; return 0;
		}
		else{
			if (!vis[i]){
				vis[i] = 1; ll ii = p[i];
				ll mx = a[i], mxdx = i;
				ll cyc_len = 1;
				while (ii != i){
					if (mx < a[ii]){
						mx = a[ii], mxdx = ii;
					}
					vis[ii] = 1; cyc_len++; 
					ii = p[ii];
				}
				strong[mxdx] = 1;
				ans += (cyc_len - 1);
			}
		}
	}
	cout << ans << "\n";
	for (ll i = 0; i < n; i++){
		if (strong[i]){
			ll ii = p[i];
			while (ii != i){
				cout << i + 1 << " " << ii + 1 << "\n"; ii = p[ii];
			}
		}
	}
}