<?php

$cups = array_map('intval', str_split(file_get_contents('input.txt')));
$min = min($cups);
$max = max($cups);

$current_index = 0;

for ($i = 0; $i < 100; $i++) {
    $current_cup = $cups[$current_index];

    $pickups = array_splice($cups, $current_index + 1, 3);

    while (count($pickups) < 3) {
        $pickups[] = array_shift($cups);
    }

    $destination = $current_cup;
    do {
        $destination--;
        if ($destination < $min) {
            $destination = $max;
        }
    } while (in_array($destination, $pickups, true));

    $destination_index = array_search($destination, $cups, true);

    array_splice($cups, $destination_index + 1, null, $pickups);
    $cups = array_values($cups);

    // replace current index at its position
    if ($cups[$current_index] !== $current_cup) {
        $diff = array_search($current_cup, $cups, true) - $current_index;
        for ($j = 0; $j < abs($diff); $j++) {
            $cups[] = array_shift($cups);
        }
    }

    echo sprintf('move %u : %s%s', $i+1, implode(' ', $cups), "\n");

    $current_index++;
    if ($current_index >= count($cups)) {
        $current_index = 0;
    }
}

// moves done, now count from 1
$index_1 = array_search(1, $cups, true);
$final_labels = '';
$current_index = $index_1 + 1;
for ($i = 1, $l = count($cups); $i < $l; $i++) {
    if ($current_index >= $l) {
        $current_index = 0;
    }
    $final_labels .= $cups[$current_index];
    $current_index++;
}

echo $final_labels;