Elevators

No attempts yetTime limit2sMemory limit256 MB

Problem

The new building of the computer engineering department has several elevators and no stairs. To make lecture rooms and offices easier to reach, the manufacturer set each elevator to stop only at certain predefined floors. Some elevators stop only at odd floors, some only at even floors. It goes further than that: the buttons inside and outside an elevator work only for the floors that elevator is assigned to stop at. Faculty members now reach their destination floors faster, but people who do not know the building, students in particular, get confused.

A person pp is on floor ii and wants to go to floor jj. To spend the least travel time, which elevator should pp take, and on which floors should pp transfer to which elevators? Write pp's travel path as i=f1f2fk=ji = f_1 \to f_2 \to \cdots \to f_k = j. The travel time to minimize is

r=1k1frfr+1\sum_{r=1}^{k-1} \left| f_r - f_{r+1} \right|

One leg frfr+1f_r \to f_{r+1} of the path is possible only when a single elevator stops at both frf_r and fr+1f_{r+1}. Write a program that helps the people who use these elevators.

Input

The input holds several test cases. The first line of a test case gives the number of elevators nn (1n101 \le n \le 10), then the source floor and the destination floor. Line ii of the next nn lines starts with mim_i (2mi1502 \le m_i \le 150), the number of floors at which elevator ii may stop, followed by mim_i floor numbers. Every floor number is a non-negative integer smaller than 150. The floor numbers are not guaranteed to be sorted.

The input ends with a line containing 0 0 0, which you do not process.

Output

For each test case, print on one line the minimum travel time needed to reach the destination floor from the source floor. A way from the source floor to the destination floor always exists.