<?php

//統一性を持たすためにやり直した
namespace egg1to3;

class Dice
{
    protected $top;
    protected $front;
    protected $left;
    protected $right;
    protected $back;
    protected $bottom;
    protected $save;

    public function __construct($arr)
    {
        $this->save = $arr;
        $this->init();
    }

    public function init()
    {
        foreach ($this->save as $key => $val) {
            $this->{$key} = (int)$val;
        }
    }

    public function parse($str)
    {
        for ($i = 0, $len = strlen($str); $i < $len; $i++) {
            $direction = $str[$i];
            $this->move($direction);
        }
    }

    //指定された面を上面に移動させる
    public function positionChange($from)
    {
        switch ($from) {
            case 'top':
                break;
            case 'front':
                $this->move('N');
                break;
            case 'right':
                $this->move('W');
                break;
            case 'left':
                $this->move('E');
                break;
            case 'back':
                $this->move('S');
                break;
            case 'bottom':
                $this->parse('NN');
                break;
        }
    }

    //90度回転させる
    public function rotate($left = false)
    {
        $this->parse($left ? 'NES' : 'NWS');
    }

    //指定された面の値を取得
    public function getNumber($from)
    {
        return $this->{$from};
    }

    //面の値を配列にして返す
    public function getArray()
    {
        $arr = array();
        foreach ($this as $key => $val) {
            if (preg_match('/top|front|right|left|back|bottom/', $key)) {
                $arr[$key] = $val;
            }
        }
        return $arr;
    }

    //同じサイコロかチェック
    public function isEqual($dice)
    {
        $arr1 = $this->getArray();
        $arr2 = $dice->getArray();
        $diff = array_diff($arr1, $arr2);
        //片方にしか存在しない値がある
        if (count($diff)) {
            return false;
        }

        $keys = explode(',', 'top,front,right,left,back,bottom');
        foreach ($keys as $key) {
            //ある面を上面に移動する
            $dice->positionChange($key);
            $cnt = 0;
            //前面を同じ値にする(合わなければ一致しない)
            while ($this->front !== $dice->getNumber('front')) {
                //前面を同じ値にできなかった
                if ($cnt++ === 4) {
                    break;
                }
                $dice->rotate();
            }

            //初期化して次の面へ
            if ($cnt === 4) {
                $dice->init();
                continue;
            }

            $arr2 = $dice->getArray();
            $diff = array_diff_assoc($arr1, $arr2);
            //全ての面が一致すれば返り値は0
            if (count($diff) === 0) {
                return true;
            }

            $dice->init();
        }

        //一致しなかった
        return false;
    }

    protected function move($direction)
    {
        $top = $this->top;
        switch ($direction) {
            case 'S':
                $this->top = $this->back;
                $this->back = $this->bottom;
                $this->bottom = $this->front;
                $this->front = $top;
                break;
            case 'N':
                $this->top = $this->front;
                $this->front = $this->bottom;
                $this->bottom = $this->back;
                $this->back = $top;
                break;
            case 'E':
                $this->top = $this->left;
                $this->left = $this->bottom;
                $this->bottom = $this->right;
                $this->right = $top;
                break;
            case 'W':
                $this->top = $this->right;
                $this->right = $this->bottom;
                $this->bottom = $this->left;
                $this->left = $top;
                break;
            default:
                new Error('error');
        }
    }
}

$line = trim(fgets(STDIN));
$nums = explode(' ', $line);
$keys = explode(',', 'top,front,right,left,back,bottom');
$arr = array_combine($keys, $nums);
fscanf(STDIN, '%s', $str);
$dice = new Dice($arr);
$dice->parse($str);
echo $dice->getNumber('top'), PHP_EOL;

Measured memory_bytes: 6720