1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5. import java.util.Scanner;
  6.  
  7.  
  8. /* IMPORTANT: Multiple classes and nested static classes are supported */
  9.  
  10. /*
  11. * uncomment this if you want to read input.
  12. import java.io.BufferedReader;
  13. import java.io.InputStreamReader;
  14. */
  15.  
  16.  
  17. class TestClass {
  18. public static void main(String args[] ) throws Exception {
  19. /*
  20. * Read input from stdin and provide input before running
  21.  
  22. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  23. String line = br.readLine();
  24. int N = Integer.parseInt(line);
  25. for (int i = 0; i < N; i++) {
  26. System.out.println("hello world");
  27. }
  28. */
  29.  
  30. Scanner sc = new Scanner(System.in);
  31. int n = sc.nextInt();
  32. Queue<Integer> queue = new LinkedList<Integer>();
  33. for(int i=0;i<n;i++){
  34. queue.add(sc.nextInt());
  35. }
  36. int[] correct = new int[n];
  37. for(int i=0;i<n;i++){
  38. correct[i] = sc.nextInt();
  39. }
  40. int time = 0;
  41. for(int i=0;i<n;i++){
  42. int num = correct[i];
  43. int a;
  44. while((a = queue.poll())!=num){
  45. queue.add(a);
  46. time++;
  47. }
  48. time++;
  49. }
  50. System.out.println(time);
  51.  
  52. }
  53. }
Language: Java 8