import java.util.*;

public class Solution {
    public int countStudents (int[] students, int[] sandwiches) {
       Queue<Integer> queue = new LinkedList<>();
       Deque<Integer> stack = new ArrayDeque<>();
       for(int i=0;i<students.length;i++){
        queue.offer(students[i]);
       }
       for(int i=sandwiches.length;i>0;i--){
        stack.push(sandwiches[i-1]);
       }
       int limit = 0;//防止队列进入死循环
       while(!stack.isEmpty()&&limit<=queue.size()){
        if(queue.peek()==stack.peek()){
            queue.poll();
            stack.pop();
            limit = 0;
            continue;
        }
        limit++;
        int student = queue.poll();
        queue.offer(student);

       }
       //System.out.println(queue.size());
       return queue.size();
    }
}