#include <bits/stdc++.h>

using namespace std;

#define rep(i,n)for(int i=0;i<(int)(n);++i)
#define fixed(n)fixed<<setprecision(n)
#define ll long long
#define ALL(a)a.begin(),a.end()
#define LLA(a)a.rbegin(),a.rend()

template<class T>void chmin(T& a,T b){
    if(a>b){
        a=b;
    }
}
template<class T>void chmax(T& a,T b){
    if(a<b){
        a=b;
    }
}

ll gcd(ll a,ll b){
    if(b==0)return a;
    else return gcd(b,a%b);
}
ll lcm(ll a,ll b){
   return a/gcd(a,b)*b;
}

const long long INF=1LL<<60;
const long long MOD=1000000007;

int main(){
    int N;cin>>N;
    vector<int>A(N);
    rep(i,N){
        cin>>A[i];
    }
    ll MIN=INF;
    for(int bit=0;bit<(1<<(N-1));++bit){
        vector<int>S(N);
        rep(i,N){
            if(N==0)S[i]=0;
            else if(bit&(1<<(i-1))){
                S[i]=1;
            }
        }
        vector<int>OR;
        ll orr=0;
        rep(i,N){
            if(S[i]==1){
                OR.push_back(orr);
                orr=A[i];
            }else{
                orr=orr|A[i];
            }
        }if(orr!=0){
            OR.push_back(orr);
        }
        ll xorr=0;
        rep(i,OR.size()){
            xorr=xorr^OR[i];
        }
        chmin(MIN,xorr);
    }
    cout<<MIN;
}
#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;

constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();

template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <std::ostream& os = std::cout>
class Prints {
private:
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
        auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) { print(args...); }
        template <typename T>
        auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) {
            for (size_t i = 0; i < sz; i++)
                os << vec[i] << (i == sz - 1 ? term : sep);
        }
        template <typename T>
        auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
            for (size_t i = 0; i < h; i++)
                for (size_t j = 0; j < w; j++)
                    os << mat[i][j] << (j == w - 1 ? term : sep);
        }

    private:
        const char *sep, *term;
        void print() const { os << term; }
        void print_rest() const { os << term; }
        template <class T, class... Tail>
        void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); }
        template <class T, class... Tail>
        void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};

Prints<> prints;
Prints<std::cerr> prints_err;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
    int N;
    cin >> N;
    Vl A(N), B(N), P(N), invP(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++) {
        int p;
        cin >> p;
        --p;
        P[i] = p;
        invP[p] = i;
    }
    Vi ord(N);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int l, int r) { return A[l] < A[r]; });
    vector<Pii> ans;
    for (ll i = 0; i < N; i++) {
        int u = ord[i];
        int to = invP[u];
        if (u == to) continue;
        if (A[u] <= B[P[u]] || A[to] <= B[P[to]]) {
            prints()(-1);
            return;
        }
        int t = P[u];
        ans.emplace_back(u, to);
        P[u] = u;
        P[to] = t;
        invP[u] = u;
        invP[t] = to;
    }

    prints()(ans.size());
    for (auto [a, b] : ans) {
        prints()(a + 1, b + 1);
    }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    std::ifstream ifs("./in_out/input.txt");
    std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    std::ofstream ofs("./in_out/output.txt");
    std::cout.rdbuf(ofs.rdbuf());
#endif
    std::cout << std::setprecision(18) << std::fixed;
    solve();
    std::cout << std::flush;
    return 0;
}