{$Q+}
program space_trash;

const
    maxlen = 100;
    maxn = 100;

var
    move: array ['A'..'Z'] of string[maxlen];
    a: array ['A'..'Z', 1..maxn] of longint;

function calc(c: char; n: longint): longint;
var
    i: longint;
begin
    if n = 1 then
    begin
        calc := 1;
        exit;
    end;

    if a[c][n] <> 0 then
    begin
        calc := a[c][n];
        exit;
    end;

    a[c][n] := 1;
    for i := 1 to length(move[c]) do
    begin
        a[c][n] := a[c][n] + calc(move[c][i], n - 1);
    end;
    calc := a[c][n];
end;

var
    c: char;
    n, r: longint;

begin
{    assign(input, 'trash.in');
    reset(input);
    assign(output, 'trash.out');
    rewrite(output);}

    readln(move['N']);
    readln(move['S']);
    readln(move['W']);
    readln(move['E']);
    readln(move['U']);
    readln(move['D']);

    read(c);
    read(n);

    r := calc(c, n);
    if r > 1000000000 then
        runerror(215);
    writeln(r);
{    outp(c, n);}

    close(input);
    close(output);
end.
