program loginis_kubas;
{ 147 u‘davinys }
  type kubas = array ['a'..'h', 'a'..'h'] of boolean;
       virsunes = array ['a'..'h'] of boolean;

  function galima (prad, gal: char; v: virsunes): boolean;
    var i, j: char;
        k: kubas;
        zingsn: integer;
  begin
    { prad‘ioje laikome, kad nei per vien… virž–nŠ negalima eiti }
    for i := 'a' to 'h' do
      for j := 'a' to 'h' do
        k[i, j] := false;
    { su‘ymime iž kuri— virž–ni—  kurias galima pakli–ti }
    k['a', 'b'] := true; k['b', 'a'] := true;
    k['a', 'd'] := true; k['d', 'a'] := true;
    k['a', 'e'] := true; k['e', 'a'] := true;
    k['c', 'b'] := true; k['b', 'c'] := true;
    k['c', 'd'] := true; k['d', 'c'] := true;
    k['c', 'g'] := true; k['g', 'c'] := true;
    k['f', 'b'] := true; k['b', 'f'] := true;
    k['f', 'e'] := true; k['e', 'f'] := true;
    k['f', 'g'] := true; k['g', 'f'] := true;
    k['h', 'e'] := true; k['e', 'h'] := true;
    k['h', 'd'] := true; k['d', 'h'] := true;
    k['h', 'g'] := true; k['g', 'h'] := true;
    { jei virž–ne draud‘iama eiti,  j… nei ateisi, nei iž jos ižeisi }
    for i := 'a' to 'h' do
      if not v[i]
         then for j := 'a' to 'h' do
                 begin
                   k[i, j] := false; k[j, i] := false;
                 end;
    { surasime visas virž–nes, pasiekiamas iž pradin‚s virž–n‚s }
    { jei virž–n‚ pasiekiama, jai prireiks ne daugiau keturi— ‘ingsni— }
    for zingsn := 1 to 3 do
      for i := 'a' to 'h' do
        for j := 'a' to 'h' do
          if k[prad, i] and k[i, j]
             then begin
                    k[prad, j] := true;
                    k[j, prad] := true;
                  end;
    galima := k[prad, gal]
  end; { galima }

  var v: virsunes;
      i, c, prad, gal: char;
begin
  { perskaitome pradinius duomenis }
  writeln ('­veskite pradinius duomenis: ');
  readln (prad);
  readln (gal);
  for i := 'a' to 'h' do
    begin
      readln (c);
      v[i] := (c = 't')
    end;
  if galima (prad, gal, v)
     then writeln ('Galima')
     else writeln ('Negalima')
end.