// Задача "Бендер"
// XI Всероссийская командная олимпиада школьников по программированию
// Автор задачи: Антон Банных
// Автор чекера: Владимир Ульянцев, ulyantsev@rain.ifmo.ru

// Не проверяет решение жюри

uses
    testlib, SysUtils, Math;
const
    MAXK = 10;
    MAXCOEFF = 1000;
type
    int = longint;

var
    n, k, r, l : int;

function emulate(a, b, c, r : int) : int;
var
    i, x, t : int;
    ar : array [1..MAXK] of int;
begin
    for i := 1 to k do begin
        ar[i] := i;
    end;

    x := c;
    for i := 1 to n do begin
        t := ar[x mod k + 1];
        ar[x mod k + 1] := ar[(x + 1) mod k + 1];
        ar[(x + 1) mod k + 1] := t;

        x := (a * x + b) mod k;
    end;

    result := -1;
    for i := 1 to k do begin
        if ar[i] = r then begin
            result := i;
            exit;
        end;
    end;
end;

var
    a, b, c : int;
    firstOufToken, firstAnsToken : string;

begin
    n := inf.readlongint();
    k := inf.readlongint();
    r := inf.readlongint();
    l := inf.readlongint();

    firstOufToken := ouf.readword(blanks, blanks);
    firstAnsToken := ans.readword(blanks, blanks);

    if lowercase(firstOufToken) = 'impossible' then begin
        if lowercase(firstAnsToken) = 'impossible' then begin
            quit(_ok, 'Ok, impossible found');
        end else begin
            quit(_wa, 'Answer expected, but impossible found');
        end;
    end; 

    ouf.reset();
    a := ouf.readlongint();
    b := ouf.readlongint();
    c := ouf.readlongint();

    if (a < 0) or (a > MAXCOEFF) then begin
        quit(_wa, '"a" is out of bounds: a = ' + inttostr(a));
    end;

    if (b < 0) or (b > MAXCOEFF) then begin
        quit(_wa, '"b" is out of bounds: b = ' + inttostr(b));
    end;

    if (c < 0) or (c > MAXCOEFF) then begin
        quit(_wa, '"c" is out of bounds: c = ' + inttostr(c));
    end;

    if emulate(a, b, c, r) <> l then begin
        quit(_wa, 'Wrong coefficients: ball with number ' + inttostr(r) + ' found in position ' + 
                  inttostr(emulate(a, b, c, r)) + ' instead of ' + inttostr(l));
    end;

    quit(_ok, 'Ok, correct answer found');
end.
