program checkc;

uses
    testlib, sysutils;

const
    maxn = 1000;

var
    nn: array [1..maxn] of longint;
    nb: array [1..maxn, 1..maxn] of longint;
    u: array [1..maxn] of boolean;
    a: array [1..maxn, 1..maxn] of longint;

procedure dfs(i, j, l: longint);
var
    k: longint;
begin
    u[j] := true;
    a[i][j] := l;
    for k := 1 to nn[j] do
        if not u[nb[j][k]] then
        begin
            dfs(i, nb[j][k], l + 1);
        end;
end;

var
    i, j, k, n, ja, pa: longint;

begin
    n := inf.readlongint;
    for i := 1 to n do
    begin
        nn[i] := inf.readlongint;
        for j := 1 to nn[i] do
            nb[i][j] := inf.readlongint;
    end;


    fillchar(a, sizeof(a), 0);
    for i := 1 to n do
    begin
        fillchar(u, sizeof(u), 0);
        dfs(i, i, 0);
    end;

    i := ans.readlongint;
    j := ans.readlongint;
    k := ans.readlongint;
    ja := a[i][j] + a[j][k] + a[k][i];

    i := ouf.readlongint;
    j := ouf.readlongint;
    k := ouf.readlongint;
    if (i = j) or (i = k) or (j = k) then
        quit(_wa, 'city numbers are not different');
    if (i < 1) or (i > n) then quit(_pe, '');
    if (j < 1) or (j > n) then quit(_pe, '');
    if (k < 1) or (k > n) then quit(_pe, '');

    pa := a[i][j] + a[j][k] + a[k][i];

    if ja > pa then
        quit(_wa, 'not optimal - expected: ' + inttostr(ja) + ', found: ' + inttostr(pa));
    if ja < pa then
        quit(_fail, 'better then jury''s - expected: ' + inttostr(ja) + ', found: ' + inttostr(pa));

    quit(_ok, inttostr(ja));
end.