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

#define llong long long 
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#ifdef testing/*{{{*/
mt19937 rng(177013);
#else
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#endif
#define rand() (int)(rng() >> 1)
#define CONCAT_(x, y) x##y
#define CONCAT(x, y) CONCAT_(x, y)
#define SPEC(name) CONCAT(name, __LINE__)
#ifdef LOCAL_DEBUG   
int __db_level = 0;
#define clog cerr << string(__db_level * 2, ' ')
struct debug_block {
    string msg;
    debug_block(const string& s): msg(s) { clog << "{ " << msg << endl; ++__db_level; }
    ~debug_block() { --__db_level; clog << "} " << msg << endl; }
};
#define DB(args...) stringstream SPEC(ss); SPEC(ss)<< args; debug_block SPEC(dbbl)(SPEC(ss).str())
#else
#define clog if (0) cerr
#define DB(...)
#endif
#define db(val) "[" #val " = " << val << "]; "
template<class U, class V> ostream& operator<<(ostream& out, const pair<U, V>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
    if constexpr(i == tuple_size<T>::value) return out << ")";
    else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U>
ostream& operator<<(ostream& out, const tuple<U...>& tup) { return print_tuple_utils<0, tuple<U...>>(out, tup); }
template<class, typename = void> struct has_const_iterator : false_type {};
template<class T> struct has_const_iterator<T, void_t<typename T::const_iterator>> : true_type {};
template<class T>
typename enable_if<has_const_iterator<T>::value && !is_same<T, string>::value, ostream&>::type
operator<<(ostream& out, const T& container) { 
    auto beg = container.begin(), end = container.end();
    out << "(" << container.size() << ") {";
    if (beg != end) out << *(beg++);
    while (beg != end) out << ", " << *(beg++);
    return out << "}";
}
#define ptrtype(x) typename iterator_traits<x>::value_type
template<class u> vector<ptrtype(u)> $v(u a, u b) { return vector<ptrtype(u)>(a, b); }/*}}}*/
// ACTUAL SOLUTION START HERE ////////////////////////////////////////////////////////////////

const int maxn = 201010;
int n;
int a[maxn], b[maxn], p[maxn], rev[maxn];

vector<pair<int, int>> ans;
void do_swap(int u, int v) {
    clog << db(u) << db(v) << endl;
    assert(a[u] > b[p[u]]);
    assert(a[v] > b[p[v]]);
    swap(p[u], p[v]);
    ans.emplace_back(u, v);
}

bool vis[maxn];

int main(void) {
#ifdef LOCAL
    freopen("main.inp", "r", stdin);
    freopen("main.out", "w", stdout);
    freopen(".log", "w", stderr);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    rep1(i, n) cin >> a[i];
    rep1(i, n) cin >> b[i];
    rep1(i, n) cin >> p[i];
    rep1(i, n) rev[p[i]] = i;
    
    
    rep1(i, n) {
        if (vis[i] or p[i] == i) continue;
        vector<int> x;
        for (int u = i; !vis[u]; u = rev[u]) {
            vis[u] = true;
            x.push_back(u);
        }
        
        clog << db(i) << db(x) << endl;
        
        for (auto u: x) if (a[u] <= b[p[u]]) {
            clog << db(u) << db(a[u]) << db(p[u]) << db(b[p[u]]) << endl;
            cout << "-1";
            return 0;
        }
        
        auto it = min_element(x.rbegin(), x.rend(), [&](int u, int v) { return b[p[u]] < b[p[v]]; });
        rotate(x.rbegin(), it, x.rend());
        
        clog << db(x) << endl;
        
        for (int prev = len(x) - 1, cur = 0; cur < len(x) - 1; prev = cur++) {
            do_swap(x[prev], x[cur]);
        }
    }
    
    cout << len(ans) << '\n';
    rep1(i, n) assert(p[i] == i);
    for (auto [u, v]: ans) cout << u << ' ' << v << '\n';
    
    return 0;
}

// vim: foldmethod=marker


#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
//using namespace atcoder;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
typedef vector<vector<int> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;

#define debug(v) cout<<#v<<": ",prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T> 
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template<typename T> 
inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } }
template <typename T> 
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T> 
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T> 
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}

/*
解法
色を頂点、カードを辺としたグラフを考える
1つの連結成分を考えると頂点数-1の色を選べそう
サイクルあれば全部選べそう

*/

ll n;
Graph g;

ll cnt;
bool cyc;

vector<ll>seen;

void dfs(int v,int p){
    for(ll x:g[v]){
        if(x==p)continue;
        if(seen[x]){
            cyc=true;
            continue;
        }
        cnt++;
        seen[x]=true;
        dfs(x,v);
    }
    return;
}

int main(){
    cin >> n;
    g.resize(400010);
    rep(i,n){
        ll a,b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    seen.resize(400010,false);
    ll ans = 0;
    rep(i,400010){
        if(seen[i])continue;
        seen[i]=true;
        cnt=1;
        cyc=false;
        dfs(i,-1);
        ans+=cnt-1;
        if(cyc)ans++;
    }
    cout << ans << endl;
    return 0;
}
