Under construction

Still being built. Things may change

Amidakuji

Interview

Time limit8sMemory limit1024 MB

Summary
Given an amidakuji and a start line p, find the position of one added bar that routes p's trace to line q, or report OK or NG.
Level

Medium4 of 10

Topics
Brute force, Simulation, Array
Solved
No attempts yet

Problem

Amidakuji is a lottery method popular in East Asia. It is often used to hand out a number of gifts to people or to assign a number of tasks.

Figure B-1 shows an example of amidakuji. A number of vertical lines are drawn, and the number of lines equals the number of participants. Some horizontal bars are drawn between pairs of adjacent vertical lines. The number and positions of the bars are arbitrary, but no two bars may share an endpoint. Usually, many horizontal bars are drawn at random.

To use an amidakuji, first write the names of the items, gifts or tasks, at the bottom ends of the vertical lines. Then each participant chooses a top end of a vertical line, all different from one another. Participants trace down the chosen line from the top. When they reach a horizontal bar, they follow it to the adjacent vertical line it connects to, and continue downward. When the bottom end of a vertical line is reached, the item written there is the gift or task awarded.

Figure B-2 shows the case where the top end P is chosen. In this case, the item at R is obtained.

Assume that the participant choosing P in Figure B-2 wants the item at Q, which is not reached. A special rule just introduced may grant that wish. The new rule allows adding a single horizontal bar at an arbitrary position. Adding the bar marked X in Figure B-3 makes the trace reach Q.

Your task is to write a program that finds the position of a horizontal bar to add to a given amidakuji, so that the trace from the top end of the specified vertical line reaches the bottom end of the specified vertical line.

Input

The input consists of multiple datasets, each in the following format.

nn mm pp qq x1x_1 ⋯\cdots xmx_m

All input items are positive integers. nn is the number of vertical lines, and mm is the number of horizontal bars in the original amidakuji. The participant choosing the pp-th line from the left wants the trace to reach the bottom end of the qq-th line from the left. It holds that 2≤n≤502 \le n \le 50, m≤500m \le 500, p≤np \le n, and q≤nq \le n.

x1,…,xmx_1, \ldots, x_m are the positions of the horizontal bars, where xkx_k is the position of the kk-th bar from the top. All bar positions are distinct. It holds that xk≤n−1x_k \le n-1, and the bar connects the xkx_k-th line and the (xk+1)(x_k+1)-th line.

The end of the input is a line with four zeros. The number of datasets does not exceed 300.

Output

For each dataset, output one of the following on a single line.

  • If qq can be reached from pp without adding a bar, output OK.
  • If not, and adding a single bar cannot make qq reachable from pp, output NG.
  • Otherwise, output two integers xx and yy separated by a space. This means a bar connecting the xx-th and (x+1)(x+1)-th lines is added at the height between the yy-th bar from the top and the (y+1)(y+1)-th bar. If the bar goes above the uppermost bar, yy is 0. If it goes below the lowermost bar, yy is mm. If there are several valid positions, output the one with the smallest yy.

OK and NG are uppercase.

Examples1

  1. Example 1

    Input
    5 8 3 4
    2 1 4 2 3 1 3 2
    5 8 3 3
    2 1 4 2 3 1 3 2
    5 8 3 1
    3 1 4 2 2 4 1 3
    4 8 1 4
    3 1 3 1 3 1 3 1
    0 0 0 0
    
    Expected output
    2 6
    OK
    NG
    2 2