This page is still under construction.

Parts of this page are still being built. What you see may change.

Forgetful Waiter

Time limit1sMemory limit128 MB

Summary
Customers sit around a round table and pass pizzas left or right each turn; find the minimum number of turns until every pizza reaches the customer who ordered it.
Level

Hard9 of 10

Topics
Graph, Greedy, Simulation, Implementation
Solved
No attempts yet

Problem

Bob is a waiter at Pizza Round, a restaurant that serves many kinds of pizza on round tables of different sizes. When a group of customers arrives, Bob seats them at a table of the right size, takes their orders, and later serves the pizzas by placing them in front of the customers so that each customer receives the type of pizza they ordered.

Unfortunately, Bob is forgetful and cannot remember what each customer ordered, so he usually puts the pizzas down in the wrong places. The customers kindly cooperate, passing the pizzas around the table over several turns until everyone has the right one. In each turn, a customer may pick up a pizza in front of them with their left hand and pass it to the neighbor on their left, and/or pick up a pizza with their right hand and pass it to the neighbor on their right (both hands may be used in the same turn). At most 5 pizzas may sit in front of any customer at once, so a pizza can be passed to a neighbor only when that neighbor still has room.

Given the type each customer ordered and the type currently in front of each customer, find the minimum number of turns needed until every customer has the pizza they ordered. This is always possible: for each type, the number of pizzas of that type on the table equals the number ordered. Only the minimum number of turns matters, not the exact passing strategy.

For example, six customers sit around a round table; the label on each customer is the type they ordered, and the number on each pizza is its type. The pizzas can be delivered in two turns, but not in one, because the single type-2 pizza needs at least two turns to reach its destination.

Write a program that computes the minimum number of turns needed to bring every pizza on the table to the customer who ordered it.

Input

The input contains several test cases. Each test case begins with a line holding an integer nn, the number of customers in the group (1<n≤10001 < n \le 1000). The customers are numbered from 11 to nn and sit around the table in counter-clockwise order, so customer 11 is adjacent to customer nn. Each of the next nn lines describes one customer: line ii contains two integers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n), where aia_i is the pizza type customer ii ordered and bib_i is the pizza type currently placed in front of customer ii. The sequence b1,…,bnb_1, \dots, b_n is a permutation of a1,…,ana_1, \dots, a_n. A line containing a single 00 terminates the input.

Output

For each test case, print a single line containing one non-negative integer: the minimum number of turns required to bring every pizza to the customer who ordered it. If the pizzas are already placed correctly, print 00.

Examples8

  1. Example 1

    Input
    6
    1 1
    3 2
    5 3
    3 5
    3 3
    2 3
    7
    7 2
    4 7
    1 4
    3 1
    5 3
    2 5
    2 2
    0
    
    Expected output
    2
    1
    
  2. Example 2

    Input
    5
    1 1
    2 2
    3 3
    4 4
    5 5
    0
    
    Expected output
    0
    
  3. Example 3

    Input
    5
    1 5
    2 1
    3 2
    4 3
    5 4
    0
    
    Expected output
    1
    
  4. Example 4

    Input
    2
    1 2
    2 1
    0
    
    Expected output
    1
    
  5. Example 5

    Input
    6
    1 4
    2 2
    3 3
    4 1
    5 5
    6 6
    0
    
    Expected output
    3
    
  6. Example 6

    Input
    4
    1 1
    1 1
    1 1
    1 1
    0
    
    Expected output
    0
    
  7. Example 7

    Input
    6
    1 2
    1 2
    1 2
    2 1
    2 1
    2 1
    0
    
    Expected output
    2
    
  8. Example 8

    Input
    5
    1 5
    2 1
    3 2
    4 3
    5 4
    2
    1 2
    2 1
    0
    
    Expected output
    1
    1