class Solution:
    def countStudents(self, students, sandwiches):
        # 记录连续不匹配的次数,防止死循环
        unmatched_count = 0
        while students and sandwiches:
            if students[0] == sandwiches[0]:
                students.pop(0)
                sandwiches.pop(0)
                unmatched_count = 0
            else:
                a = students.pop(0)
                students.append(a)
                unmatched_count += 1
                if unmatched_count >= len(students) + 1:  
                    # +1是因为我们刚刚pop了一个
                    break
        return len(students)