<?php
$labels1 = fscanf(STDIN, '%d %d %d %d %d %d:w');
$labels2 = fscanf(STDIN, '%d %d %d %d %d %d:w');

$directions = array(
    '-',
    'N',
    'S',
    'E',
    'W',
    array('N', 'N'),
);

$dice1 = new Dice($labels1);
$dice2 = new Dice($labels2);

foreach ($directions as $direction) {
    $dice2->setDefault()->moveTo($direction);

    if ($dice1->equalsTo($dice2)) {
        echo "Yes\n";
        exit;
    }
}

echo "No\n";

class Dice
{
    private $labels;

    private $top;
    private $bottom;
    private $front;
    private $back;
    private $left;
    private $right;

    public function __construct(array $labels=array(1,2,3,4,5,6))
    {
        $this->labels = $labels;
        $this->setDefault();
    }

    public function setDefault()
    {
        $this->top    = 1;
        $this->bottom = 6;
        $this->front  = 2;
        $this->back   = 5;
        $this->left   = 4;
        $this->right  = 3;

        return $this;
    }

    public function moveTo($direct)
    {
        $directions = is_array($direct) ? $direct : array($direct);

        foreach ($directions as $dir) {
            switch ($dir) {
            case 'N':
                $this->moveToUp();
                break;
            case 'S':
                $this->moveToDown();
                break;
            case 'E':
                $this->moveToRight();
                break;
            case 'W':
                $this->moveToLeft();
                break;
            }
        }

        return $this;
    }

    public function moveToDown()
    {
        $this->front  = $this->top;
        $this->top    = $this->back;
        $this->back   = 7 - $this->front;
        $this->bottom = 7 - $this->top;

        return $this;
    }

    public function moveToUp()
    {
        $this->back   = $this->top;
        $this->top    = $this->front;
        $this->front  = 7 - $this->back;
        $this->bottom = 7 - $this->top;

        return $this;
    }

    public function moveToRight()
    {
        $this->right  = $this->top;
        $this->top    = $this->left;
        $this->left   = 7 - $this->right;
        $this->bottom = 7 - $this->top;

        return $this;
    }

    public function moveToLeft()
    {
        $this->left   = $this->top;
        $this->top    = $this->right;
        $this->right  = 7 - $this->left;
        $this->bottom = 7 - $this->top;

        return $this;
    }

    public function rotateToRight()
    {
        $this->right = $this->back;
        $this->back  = $this->left;
        $this->left  = 7 - $this->right;
        $this->front = 7 - $this->back;

        return $this;
    }

    public function rotateToLeft()
    {
        $this->left  = $this->back;
        $this->back  = $this->right;
        $this->right = 7 - $this->left;
        $this->front = 7 - $this->back;

        return $this;
    }

    public function top()
    {
        return $this->label($this->top);
    }

    public function bottom()
    {
        return $this->label($this->bottom);
    }

    public function front()
    {
        return $this->label($this->front);
    }

    public function back()
    {
        return $this->label($this->back);
    }

    public function left()
    {
        return $this->label($this->left);
    }

    public function right()
    {
        return $this->label($this->right);
    }

    public function equalsTo(Dice $dice)
    {
        if ($this->top() != $dice->top()) {
            return false;
        }

        if ($this->bottom() != $dice->bottom()) {
            return false;
        }

        $around1  = $this->left().$this->front().$this->right().$this->back();
        $around1 .= $around1;

        $around2 = $dice->left().$dice->front().$dice->right().$dice->back();

        return false !== strpos($around1, $around2);
    }

    private function label($index)
    {
        return $this->labels[$index-1];
    }

    private function index($label)
    {
        foreach ($this->labels as $index => $val) {
            if ($val == $label) {
                return $key + 1;
            }
        }
    }
}

Measured memory_bytes: 6740