import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

public class virus_sm {

    class Point {
        int id;
        int enter1, leave1, enter2, leave2;
        int res1, res2;

        Point(int id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "Point{" +
                    "id=" + id +
                    ", enter1=" + enter1 +
                    ", leave1=" + leave1 +
                    ", enter2=" + enter2 +
                    ", leave2=" + leave2 +
                    ", res1=" + res1 +
                    ", res2=" + res2 +
                    '}';
        }
    }

    void readTree(int n, Point[] points, boolean isFirst) {
        ArrayList<Integer>[] edges = new ArrayList[n];
        int root = -1;
        for (int i = 0; i < n; i++) {
            edges[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < n; i++) {
            int from = in.nextInt() - 1;
            if (from < 0) {
                root = i;
            } else {
                edges[from].add(i);
            }
        }
        timer = 0;
        dfs(root, edges, points, isFirst);
    }

    int timer = 0;

    private void dfs(int v, ArrayList<Integer>[] edges, Point[] points, boolean isFirst) {
        if (isFirst) {
            points[v].enter1 = timer++;
        } else {
            points[v].enter2 = timer++;
        }

        for (int to : edges[v]) {
            dfs(to, edges, points, isFirst);
        }
        if (isFirst) {
            points[v].leave1 = timer++;
        } else {
            points[v].leave2 = timer++;
        }
    }

    class Fenwick {
        int[] a;

        Fenwick(int n) {
            a = new int[n + 1];
        }

        void inc(int x) {
            x++;
//            System.err.println("inc " + x);
            for (int i = x; i < a.length; i |= i + 1) {
                a[i]++;
//                System.err.println(" a[" + i + "]++");
            }
        }

        int sum(int x) {
            x++;
            int res = 0;
            for (int i = x; i > 0; i = (i & (i + 1)) - 1) {
                res += a[i];
//                System.err.println(" res += a[" + i + "]");
            }
//            System.err.println("sum " + x + "  = " + res);
            return res;
        }

        int sum(int L, int R) {
            int res = sum(R) - sum(L - 1);
//            System.err.println("sum " + L + " " + R + " = " + res);
            return res;
        }


    }

    class Event {
        int type;
        Point p;
        int x;

        Event(int type, Point p, int x) {
            this.type = type;
            this.p = p;
            this.x = x;
        }

    }

    final int INC = 1;
    final int REQUEST = 2;

    void solve() throws IOException {
        int n = in.nextInt();
        Point[] ps = new Point[n];
        for (int i = 0; i < ps.length; i++) {
            ps[i] = new Point(i);
        }
        readTree(n, ps, true);
        readTree(n, ps, false);
        Fenwick f1 = new Fenwick(2 * n);
        Arrays.sort(ps, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return -o1.enter1 + o2.enter1;
            }
        });
        for (Point p : ps) {
            f1.inc(p.enter2);
//            System.err.println("inc " + p.enter2);
//            System.err.println(" " + p.enter2 + " " + p.leave2);
//            System.err.println("  " + f1.sum(p.enter2, p.leave2));
            p.res1 = f1.sum(p.enter2, p.leave2);
        }


        ArrayList<Event> events = new ArrayList<Event>();
        for (Point p : ps) {
            events.add(new Event(INC, p, p.enter1));
            events.add(new Event(REQUEST, p, p.leave1));
        }
        Collections.sort(events, new Comparator<Event>() {
            @Override
            public int compare(Event e1, Event e2) {
                if (e1.x != e2.x) {
                    return -e1.x + e2.x;
                }
                return e1.type - e2.type;
            }
        });
        f1 = new Fenwick(2 * n);
        for (Event e : events) {
            if (e.type == INC) {
                f1.inc(e.p.enter2);
            } else if (e.type == REQUEST) {
                e.p.res2 = f1.sum(e.p.enter2, e.p.leave2);
            }
        }

        long sum = 0;
        for (Point p : ps) {
//            System.err.println(p.id + " " + p.res1 + " " + p.res2);
            sum += p.res1 - p.res2;
        }
        out.println(sum - n);
        for (Point p : ps) {
//            System.err.println(p);
        }
    }


    Scanner in;
    PrintWriter out;

    void run() throws IOException {
        in = new Scanner(new FileReader("virus.in"));
        out = new PrintWriter("virus.out");
        solve();
        in.close();
        out.close();
    }


    public static void main(String[] args) throws IOException {
        new virus_sm().run();
    }


}
/*
(enter1, enter2)
enter1 <= x <= leave1
enter2 <= y <= leave2

*/
