Under construction

Still being built. Things may change

Cube Net

Time limit1sMemory limit128 MB

Summary
Given a hexomino net on a 6x6 grid with faces numbered 1 to 6, determine if it folds into a valid cube and if so output the face opposite face 1, otherwise output 0.
Level

Medium7 of 10

Topics
Simulation, Geometry, Implementation
Solved
No attempts yet

Problem

Consider whether the dotted edges of a given net can be folded to form a die-shaped cube. Each face of the net is marked with a distinct integer from 1 to 6.

Net (1)Net (2)

Net (1) can be folded into a cube, but net (2) cannot. Write a program that determines whether the input net can be folded into a cube.

Input

The input consists of 6 lines. Each line contains 6 integers from 0 to 6, separated by single spaces. The numbers 1 through 6 represent the six faces of the net, and 0 represents an empty cell outside the net.

Output

If the input net can be folded into a cube, print the number written on the face opposite the face marked 1. If it cannot be folded into a cube, print 0.

Examples1

  1. Example 1

    Input
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 5 0 0 0
    0 1 2 3 4 0
    0 0 6 0 0 0
    0 0 0 0 0 0
    
    Expected output
    3