LearnPy Hub
← Arena
AdvancedRating 1800

Sliding Tile Puzzle

A 2 × 3 board holds tiles 1–5 and an empty space 0. A move slides a tile next to the space into it. Print the fewest moves to reach 1 2 3 4 5 0 or -1 if impossible.

Input

Two rows of three numbers.

Output

One integer.

Sample 1

Input
1 2 3
4 0 5
Output
1

Sample 2

Input
1 2 3
5 4 0
Output
-1
solution.py
Output appears here
Sliding Tile Puzzle · Python Arena problem · LearnPy Hub