1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <bits/stdc++.h>

using namespace std;

#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;

int my_abs(int x) {
    if (x < 0) {
        return -x;
    }
    return x;
}

struct Tree {
    int size;
    int root;
    vi parent;
    vi left;
    vi right;

    Tree(int _size) : size(_size), parent(_size + 1), left(_size + 1), right(_size + 1) {}

    void read() {

        FOR(i, 1, size) {
            int p;
            cin >> p;
            // DEB("p=" << p << endl);
            if (p == -1) {
                p = 0;
            }
            parent[i] = p;
            if (p == 0) {
                root = i;
            } else if (i < p) {
                left[p] = i;
            } else if (i > p) {
                right[p] = i;
            } else {
                assert(false);
            }
        }
    }

    void rotate_left(int x) {
        int y = right[x];
        DEB("rotate_left x=" << x << " y=" << y << "\n");
        assert(y != 0);
        if (parent[x] != 0) {
            if (right[parent[x]] == x) {
                right[parent[x]] = y;
            } else {
                left[parent[x]] = y;
            }
        }
        parent[y] = parent[x];
        parent[x] = y;
        assert(left[y] == 0);
        left[y] = x;
        right[x] = 0;
    }

    void rotate_right(int x) {
        int y = left[x];
        DEB("rotate_right x=" << x << " y=" << y << "\n");
        assert(y != 0);
        if (parent[x] != 0) {
            if (right[parent[x]] == x) {
                right[parent[x]] = y;
            } else {
                left[parent[x]] = y;
            }
        }
        parent[y] = parent[x];
        parent[x] = y;
        assert(right[y] == 0);
        right[y] = x;
        left[x] = 0;
    }

    void rotate_up(int x) {
        int p = parent[x];
        DEB("rotate_up x=" << x << " parent=" << p << "\n");

        assert(p != 0);
        if (x < p) {
            rotate_right(p);
        } else if (x > p) {
            rotate_left(p);
        } else {
            assert(false);
        }
    }

    // int move_up_to_neighbor(int x, int y) {
    //     DEB("move to neighbor x=" << x << " y=" << y << "\n");
    //     assert(my_abs(x - y) == 1);
    //     int result = 0;
    //     while (parent[y] != x) {
    //         rotate_up(x);
    //         ++result;
    //     }
    //     return result;
    // }

    // int move_up_to(int x, int y) {
    //     DEB("move_up_to x=" << x << " y=" << y << endl);
    //     int result = 0;
    //     if (y > x) {
    //         FORD(i, y - 1, x) { result += move_up_to_neighbor(i, i + 1); }
    //     } else if (y < x) {
    //         FOR(i, y + 1, x) { result += move_up_to_neighbor(i, i - 1); }
    //     } else {
    //         assert(false);
    //     }
    //     return result;
    // }

    int pull_from_down(int x, int y) {
        DEB("pull_from_down x=" << x << " y=" << y << endl);
        assert(x != 0 and y != 0 and x != y);
        int result = 0;
        if (y > x) {
            if (abs(x - y) == 1) {
                if (right[x] != y) {
                    result += pull_from_down(right[x], y);
                }
                rotate_up(right[x]);
                ++result;
            } else {
                FOR(i, x, y - 1) { result += pull_from_down(i, i + 1); }
            }
        } else if (y < x) {
            if (abs(x - y) == 1) {
                if (left[x] != y) {
                    result += pull_from_down(left[x], y);
                }
                rotate_up(left[x]);
                ++result;
            } else {
                FORD(i, x, y + 1) { result += pull_from_down(i, i - 1); }
            }
        } else {
            assert(false);
        }
        DEB("pull_from_down END x=" << x << " y=" << y << endl);
        return result;
    }

    int convert_other(int target_root, Tree &other, int other_root) const {
        DEB("convert_other target=" << target_root << " other=" << other_root << endl);
        int result = 0;
        if (target_root == other_root) {
            ;
        } else {
            result += other.pull_from_down(other_root, target_root);
        }
        if (left[target_root] != 0) {
            result += convert_other(left[target_root], other, other.left[target_root]);
        }
        if (right[target_root] != 0) {
            result += convert_other(right[target_root], other, other.right[target_root]);
        }
        return result;
    }
};

void inline one() {
    int n;
    cin >> n;
    DEB("n=" << n << endl);
    Tree a(n), b(n);
    DEB("tree objects created" << endl);
    a.read();
    b.read();
    int result = b.convert_other(b.root, a, a.root);
    cout << result << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // int z; cin >> z; while(z--)
    one();
}