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
#include <cstdio>
#include <queue>
#include <vector>
#include <set>

using namespace std;

vector<pair<int, pair<int, int>>> left_rotate(vector<pair<int, pair<int, int>>> tree, int v)
{
  vector<pair<int, pair<int, int>>> calc;

  if (tree[v].second.second == -1) return calc;
  if (tree[tree[v].second.second].second.first != -1) return calc;

  int w = tree[v].second.second;
  if (tree[v].first != -1)
  {
    if (tree[tree[v].first].second.second == v)
    {
      tree[tree[v].first].second.second = w;
    }
    else
    {
      tree[tree[v].first].second.first = w;
    }
  }
  tree[w].first = tree[v].first;
  tree[v].first = w;
  tree[w].second.first = v;
  tree[v].second.second = -1;

  return tree;
}

vector<pair<int, pair<int, int>>> right_rotate(vector<pair<int, pair<int, int>>> tree, int v)
{
  vector<pair<int, pair<int, int>>> calc;
    
  if (tree[v].second.first == -1) return calc;
  if (tree[tree[v].second.first].second.second != -1) return calc;

  int w = tree[v].second.first;

  if (tree[v].first != -1)
  {
    if (tree[tree[v].first].second.second == v)
    {
      tree[tree[v].first].second.second = w;
    }
    else
    {
      tree[tree[v].first].second.first = w;
    }
  }

  tree[w].first = tree[v].first;
  tree[v].first = w;
  tree[w].second.second = v;
  tree[v].second.first = -1;
  
  return tree;
}

set<vector<pair<int, pair<int, int>>>> vis;

int bfs(vector<pair<int, pair<int, int>>> u, vector<pair<int, pair<int, int>>> t, int n)
{
  queue<pair<vector<pair<int, pair<int, int>>>, int>> q;
  
  q.push({u, 0});
  
  while (!q.empty())
  {
    vector<pair<int, pair<int, int>>> v = q.front().first;
    int d = q.front().second;
    q.pop();
    
    if (v == t) return d;
    
    for (int i = 1; i <= n; i++)
    {
      vector<pair<int, pair<int, int>>> v1 = left_rotate(v, i);

      if (v1.size())
      {
        if (vis.find(v1) == vis.end())
        {
          vis.insert(v1);
          q.push({v1, d + 1});
        }
      }
      v1 = right_rotate(v, i);
      
      if (v1.size())
      {
        if (vis.find(v1) == vis.end())
        {
          vis.insert(v1);
          q.push({v1, d + 1});
        }
      }
    }
  }
}

int main()
{
  int n;
  scanf("%d", &n);
  
  vector<pair<int, pair<int, int>>> s, t;
  
  for (int i = 0; i <= n; i++)
  {
    s.push_back({-1,{-1,-1}});
    t.push_back({-1,{-1,-1}});
  }
  
  for (int i = 1; i <= n; i++)
  {
    int a;
    scanf("%d", &a);
    s[i].first = a;
    if (a == -1) continue;
    if (i < a)
    {
      s[a].second.first = i;
    }
    else
    {
      s[a].second.second = i;
    }
  }
  
  for (int i = 1; i <= n; i++)
  {
    int a;
    scanf("%d", &a);
    t[i].first = a;
    if (a == -1) continue;
    if (i < a)
    {
      t[a].second.first = i;
    }
    else
    {
      t[a].second.second = i;
    }
  }
  
  int ret = bfs(s, t, n);
  
  printf("%d\n", ret);
  
  return 0;
}