{$apptype console}
{$q+,r+,o-}

uses
    TestLib, SysUtils;

const
    max_n = 1000;
    max_m = 10000;

var
    p: array [1..max_n] of longint;
    a, b, l: array [1..max_m] of longint;
    i, n, m: longint;
    p5, q5, p6, q6, l5, l6: int64;
    e, t: longint;
    jas, pas: string;
    ja, pa: longint;

function get(i: longint): longint;
begin
    if p[i] <> i then begin
        p[i] := get(p[i]);
    end;
    get := p[i];
end;

procedure join(i, j: longint);
begin
    i := get(i);
    j := get(j);
    if (i < j) then begin
        p[j] := i;
    end else begin
        p[i] := j;
    end;
end;

begin
    n := inf.readLongint();
    m := inf.readLongint();

    for i := 1 to n do begin
        p[i] := i;
    end;

    for i := 1 to m do begin
        a[i] := inf.readLongint();
        b[i] := inf.readLongint();
        l[i] := inf.readLongint();
    end;
    p5 := inf.readLongint();
    q5 := inf.readLongint();
    p6 := inf.readLongint();
    q6 := inf.readLongint();

    jas := ans.readstring();
    pas := ouf.readstring();
    if (jas <> 'Impossible') and (pas = 'Impossible') then begin
        quit(_wa, 'Answer exists, but not found');
    end;
    if (jas = 'Impossible') and (pas = 'Impossible') then begin
        quit(_ok, 'Impossible');
    end;

    ouf.reset();
    pa := ouf.readLongint();

    l5 := 0;
    l6 := 0;
    for i := 1 to n - 1 do begin
        e := ouf.readLongint();
        t := ouf.readLongint();
        if (e < 1) or (e > m) then begin
            quit(_wa, format('Link %d does not exist', [e]));
        end;
        if (t = 5) then begin
            l5 := l5 + l[e];
        end else if (t = 6) then begin
            l6 := l6 + l[e];
        end else begin
            quit(_wa, format('Invalid cable type: %d', [t]));
        end;
        if (get(a[e]) = get(b[e])) then begin
            quit(_wa, format('Cable %d forms a cycle', [e]));
        end;
        join(a[e], b[e]);
    end;

    for i := 1 to n do begin
        if get(i) <> 1 then begin
            quit(_wa, format('Apartments 1 and %d are not connected', [i]));
        end;
    end;

    if (l5 > q5) then begin
        quit(_wa, format('Cable 5 used %d, available %d', [l5, q5]));
    end;
    if (l6 > q6) then begin
        quit(_wa, format('Cable 6 used %d, available %d', [l6, q6]));
    end;
    if (p5 * l5 + p6 * l6 <> pa) then begin
        quit(_wa, format('Declared cost %d, actual cost %d', [pa, p5 * l5 + p6 * l6]));
    end;

    if jas = 'Impossible' then begin
        quit(_fail, 'Solution not found by jury');
    end;

    ja := strToInt(jas);
    if (pa > ja) then begin
        quit(_wa, format('Cost %d, optimal cost %d', [pa, ja]));
    end;
    if (pa < ja) then begin
        quit(_fail, format('Cost %d, jury cost %d', [pa, ja]));
    end;

    quit(_ok, format('l5=%d, l6=%d, cost=%d', [l5, l6, pa]));
end.
