output_description: Print the maximum possible distance of A and B.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      P_0 P_1 \cdots P_{N-1}
      Q_0 Q_1 \cdots Q_{N-1}
attempt: from collections import deque
  
  
  # 枝を表すクラス
  # altは逆の枝を表す
  class Edge:
  
      def __init__(self, from_v, to_v, cap):
          self.from_v = from_v
          self.to_v = to_v
          self.cap = cap
          self.alt = None
  
  
  # 頂点を表すクラス
  class Vertex:
  
      def __init__(self, id):
          self.id = id
          self.edge_list = []
  
  
  # グラフを表すクラス
  class Graph:
  
      # 初期化する
      # vnumは頂点数を表す
      # s_vは始点、t_vは終点を表す
      def __init__(self, vnum):
          self.__vertex_list = [Vertex(i) for i in range(vnum + 2)]
          self.__edge_list = []
  
      # 枝を追加する
      def add_edge(self, from_id, to_id, cap):
          from_v = self.vertex(from_id)
          to_v = self.vertex(to_id)
          normal_edge = Edge(from_v, to_v, cap)
          reverse_edge = Edge(to_v, from_v, 0)
          normal_edge.alt = reverse_edge
          reverse_edge.alt = normal_edge
  
          from_v.edge_list.append(normal_edge)
          to_v.edge_list.append(reverse_edge)
          self.__edge_list.append(normal_edge)
          self.__edge_list.append(reverse_edge)
  
      def vertex(self, id):
          return self.__vertex_list[id]
  
      # 辺の状況をprintする
      def print_edge(self):
          for i in range(len(self.__edge_list)):
              print("[f_v={}, ".format(self.__edge_list[i].from_v.id), end="")
              print("t_v={}, ".format(self.__edge_list[i].to_v.id), end="")
              print("cap={}]".format(self.__edge_list[i].cap))
  
  
  #        print(self.__edge_list.from_v)
  #        print(self.__edge_list.to_v)
  #        print(self.__edge_list.cap)
  #        print(self.__edge_list.alt)
  
  
  class MaxFlow:
  
      #
      def __init__(self, graph, s_id, t_id):
          self.graph = graph
          self.s_v = graph.vertex(s_id)
          self.t_v = graph.vertex(t_id)
          self.dq = deque()
          self.from_dict = dict()
  
      def calc(self):
          f = 0
          while self.find_path():
              f += 1
  
              # デバッグ用
              self.graph.print_edge()
              print("f = {}".format(f))
          #              self.from_dict.clear()
          #              self.from_dict = dict()
  
          return f
  
      # sからtへのパスを一つ見つけ、そのパス上の枝の容量を0にし、
      # 逆の枝の容量を1にする
      # 経路が見つかればTrue,見つからなければFalseを返す
      def find_path(self):
          # -1はfrom_idとして不正な値
          self.put_q(self.s_v, None)
          s_id = self.s_v.id
          t_id = self.t_v.id
  
          while len(self.dq) > 0:
              v = self.dq.popleft()
              v_id = v.id
  
              #            print(self.from_dict)
  
              if v_id == t_id:
                  # from_dictを使ってs_vまでの経路をたどる
                  while v_id != s_id:
                      n_e = self.from_dict[v_id]
                      r_e = n_e.alt
                      n_e.cap = 0
                      r_e.cap = 1
                      v_id = n_e.from_v.id
                  return True
  
              # vに接続した枝eのうち、以下の条件を満たしたものを探す
              # * eの容量が1である
              # * e.toがfrom_dictに含まれていない
              for e in v.edge_list:
                  if e.cap == 0:
                      continue
                  if e.to_v in self.from_dict:
                      continue
                  self.put_q(e.to_v, e)
  
          return False
  
      # vをqに積む、from_dictにvに至る枝eを記録する
      def put_q(self, v, e):
          self.dq.append(v)
          self.from_dict[v.id] = e
  
  
  if __name__ == "__main__":
  
      def make_graph1():
          g1 = Graph(2)
          s1 = 0
          t1 = 1
          g1.add_edge(s1, t1, 1)
          return g1, s1, t1
  
      g1, s1, t1 = make_graph1()
      mf1 = MaxFlow(g1, s1, t1)
      f1 = mf1.calc()
      print("f1 ={}".format(f1))
  
      def make_graph2():
          g = Graph(4)
          s = 0
          a = 1
          b = 2
          t = 3
          g.add_edge(s, a, 1)
          g.add_edge(s, b, 1)
          g.add_edge(a, b, 1)
          g.add_edge(a, t, 1)
          g.add_edge(b, t, 1)
  
          return g, s, t
  
      g2, s2, t2 = make_graph2()
      mf2 = MaxFlow(g2, s2, t2)
      print("f2 = {}".format(mf2.calc()))
  
problem_description: Statement
  
  Snuke has permutations (P_0,P_1,\cdots,P_{N-1}) and (Q_0,Q_1,\cdots,Q_{N-1})
  of (0,1,\cdots,N-1).
  
  Now, he will make new permutations A and B of (0,1,\cdots,N-1), under the
  following conditions:
  
    * For each i (0 \leq i \leq N-1), A_i should be i or P_i.
    * For each i (0 \leq i \leq N-1), B_i should be i or Q_i.
  
  Let us define the distance of permutations A and B as the number of indices i
  such that A_i \neq B_i. Find the maximum possible distance of A and B.
samples: [{"input": "4\n    2 1 3 0\n    0 2 3 1", "output": "3\n    \n\nFor example, if we make A=(0,1,2,3) and B=(0,2,3,1), the distance will be 3,\nwhich is the maximum result possible.\n\n* * *"}, {"input": "10\n    0 4 5 3 7 8 2 1 9 6\n    3 8 5 6 4 0 2 1 7 9", "output": "8\n    \n\n* * *"}, {"input": "32\n    22 31 30 29 7 17 16 3 14 9 19 11 2 5 10 1 25 18 15 24 20 0 12 21 27 4 26 28 8 6 23 13\n    22 3 2 7 17 9 16 4 14 8 19 26 28 5 10 1 25 18 15 13 11 0 12 23 21 20 29 24 27 6 30 31", "output": "28"}]