# Task

The function `fold_cube(number_list)` should return a boolean based on a net (given as a number list), if the net can fold into a cube. 
Your code must be effecient and complete the tests within 500ms. 

## Input

Imagine a net such as the one below.

```
@
@ @ @ @
    @
```
Then, put it on the table with each `@` being one cell.

```
--------------------------
|  1 |  2 |  3 |  4 |  5 |
--------------------------
|  6 |  7 |  8 |  9 | 10 |
--------------------------
| 11 | 12 | 13 | 14 | 15 |
--------------------------
| 16 | 17 | 18 | 19 | 20 |
--------------------------
| 21 | 22 | 23 | 24 | 25 |
--------------------------
```
The number list for a net will be the numbers the net falls on when placed on the table. Note that the numbers in the list won't always be in order. The position of the net can be anywhere on the table, it can also be rotated.

For the example above, a possible number list will be `[1, 7, 8, 6, 9, 13]`.

## Output

`fold_cube` should return `True` if the net can be folded into a cube. `False` if not.

---

# Examples

### Example 1:

`number_list` = `[24, 20, 14, 19, 18, 9]`

Shape and rotation of net:
```
  @
  @
@ @ @
  @
```
Displayed on the table:
```
--------------------------
|  1 |  2 |  3 |  4 |  5 |
--------------------------
|  6 |  7 |  8 |  @ | 10 |
--------------------------
| 11 | 12 | 13 |  @ | 15 |
--------------------------
| 16 | 17 |  @ |  @ |  @ |
--------------------------
| 21 | 22 | 23 |  @ | 25 |
--------------------------
```
So, `fold_cube([24, 20, 14, 19, 18, 9])` should return `True`.

### Example 2:

`number_list` = `[1, 7, 6, 17, 12, 16]`

Shape and rotation of net:
```
@
@ @
  @ 
@ @
```
Displayed on the table:
```
--------------------------
|  @ |  2 |  3 |  4 |  5 |
--------------------------
|  @ |  @ |  8 |  9 | 10 |
--------------------------
| 11 |  @ | 13 | 14 | 15 |
--------------------------
|  @ |  @ | 18 | 19 | 20 |
--------------------------
| 21 | 22 | 23 | 24 | 25 |
--------------------------
```
`fold_cube([1, 7, 6, 17, 12, 16])` should return `False`.

#### *Translations appreciated!!*

---

### If you liked this kata.... check out these!             
- [Folding a 4D Cube (Tesseract)](https://www.codewars.com/kata/5f3b561bc4a71f000f191ef7)  
- [Wraping a net around a cube](https://www.codewars.com/kata/5f4af9c169f1cd0001ae764d)

Difficulty: introductory

Starter code:

def fold_cube(nums):
	

Solutions:
def fold_cube(nums):
    return expand(nums.pop(), set(nums), 1, 2, 3) == {1, 2, 3, -1, -2, -3}


def expand(val, nums, x, y, z):
    dirs = {z}
    for num in nums.copy():
        if abs(val - num) not in (1, 5) or {val % 5, num % 5} == {0, 1}:
            continue

        nums.discard(num)
        diff = val - num
        sign = diff // abs(diff)
        nx, ny, nz = (x, z * sign, -y * sign) if abs(diff) == 1 else (-z * sign, y, x * sign)
        dirs |= expand(num, nums, nx, ny, nz)
    return dirs

def fold_cube(nums):
    stack = [nums[0]]
    for _ in range(6):
        for i in range(len(stack)):
            if ((stack[i]-1) % 5 > 0) and ((stack[i] - 1) in nums) and ((stack[i] - 1) not in stack):
                stack.append(stack[i] - 1)
            if ((stack[i]-1) % 5 < 4) and ((stack[i] + 1) in nums) and ((stack[i] + 1) not in stack):
                stack.append(stack[i] + 1)
            if ((stack[i]-1) > 5) and ((stack[i] - 5) in nums) and ((stack[i] - 5) not in stack):
                stack.append(stack[i] - 5)
            if ((stack[i]-1) < 21) and ((stack[i] + 5) in nums) and ((stack[i] + 5) not in stack):
                stack.append(stack[i] + 5)
    print(stack)
    print(nums)
    
    
    if len(stack) != 6:
        return False
    cols = []
    for n in stack:
        if ((n-1) % 5) not in cols:
            cols.append((n-1) % 5)
    rows = []
    for n in stack:
        if (int((n-1) / 5)) not in rows:
            rows.append(int((n-1) / 5))
#     print()
#     print(rows)
#     print()
#     print(cols)
    if len(rows) + len(cols) != 7:
        return False
    if len(rows) == 2:
        if (sum(rows)+1)/2*5+ sum(cols)/5+1 not in stack or (sum(rows)-1)/2*5+ sum(cols)/5+1 not in stack:
            return False
    if len(rows) == 3:
        if sum(rows)/3*5+(sum(cols)+2)/4+1 not in stack or sum(rows)/3*5+(sum(cols)-2)/4+1 not in stack:
            print((3))
            return False
    if len(rows) == 4:
        if (sum(rows)+2)/4*5+sum(cols)/3+1 not in stack or (sum(rows)-2)/4*5+sum(cols)/3+1 not in stack:
            print((4))
            return False
    if len(rows) == 5:
        if sum(rows)+ (sum(cols)+1)/2+1 not in stack or sum(rows)+ (sum(cols)-1)/2+1 not in stack:
            print((5))
            return False
    if len(rows) == 2:
        if sum(stack)%30 != 3:
            return False
    if len(rows) == 5:
        if sum(stack)%6 != 3:
            return False
    print(True)
    return True
            
        
        
    


# Today I shall be mostly using geometry, group theory, and massive overkill to solve a problem... 

# These are the archetypes of the nets from a cube
nets = [
    [
        [1, 0, 0, 0],
        [1, 1, 1, 1],
        [1, 0, 0, 0],
    ],
    [
        [1, 0, 0, 0],
        [1, 1, 1, 1],
        [0, 1, 0, 0],
    ],
    [
        [1, 0, 0, 0],
        [1, 1, 1, 1],
        [0, 0, 1, 0],
    ],
    [
        [1, 0, 0, 0],
        [1, 1, 1, 1],
        [0, 0, 0, 1],
    ],
    [
        [0, 1, 0, 0],
        [1, 1, 1, 1],
        [0, 0, 1, 0],
    ],
    [
        [0, 1, 0, 0],
        [1, 1, 1, 1],
        [0, 1, 0, 0],
    ],
    [
        [1, 1, 0, 0],
        [0, 1, 1, 1],
        [0, 1, 0, 0],
    ],
    [
        [1, 1, 0, 0],
        [0, 1, 1, 1],
        [0, 0, 1, 0],
    ],
    [
        [1, 0, 0, 0],
        [1, 1, 1, 0],
        [0, 0, 1, 1],
    ],
    [
        [1, 1, 0, 0],
        [0, 1, 1, 0],
        [0, 0, 1, 1],
    ],
    [
        [1, 1, 1, 0, 0],
        [0, 0, 1, 1, 1],
    ],
]

# Various transformations we can apply to the nets...
def h_mirror(m):
    # Mirror a matrix horizontally
    return m[::-1]

def v_mirror(m):
    # Mirror a matrix vertically
    return [r[::-1] for r in m]
    
def transpose(m):
    # Transpose a matrix
    return list(map(list, zip(*m)))

def rotate_cw(m):
    # Rotate a matrix clock-wise
    coords = [(x, y) for y, row in enumerate(m) for x, v in enumerate(row) if v == 1]
    new_coords = normalise([(y, -x) for x, y in coords])
    result = [[0 for _ in range(len(m))] for _ in range(len(m[0]))]
    for x, y in new_coords:
        result[y][x] = 1
    return result

def rotate_ccw(m):
    # Rotate a matrix counter lock-wise
    coords = [(x, y) for y, row in enumerate(m) for x, v in enumerate(row) if v == 1]
    new_coords = normalise([(-y, x) for x, y in coords])
    result = [[0 for _ in range(len(m))] for _ in range(len(m[0]))]
    for x, y in new_coords:
        result[y][x] = 1
    return result

def identity(coords):
    # No transformation
    return coords

def normalise(coords):
    # Translate all coordinates to that all x>=0 and y>=0
    min_x = min(x for x, y in coords)
    min_y = min(y for x, y in coords)
    return tuple([(x - min_x, y - min_y) for x, y in coords])

def compose(*fns):
    # Compose transformations
    def apply(*args, **kwargs):
        result = fns[0](*args, **kwargs)
        for fn in fns[1:]: 
            result = fn(result)
        return result
    return apply

# Transformations needed to generate all the symmetries of the nets
compositions = [
    identity,
    h_mirror,
    v_mirror,
    transpose,
    rotate_cw,
    rotate_ccw,
    compose(rotate_cw, rotate_cw),
    compose(rotate_cw, rotate_cw, transpose),
]

# Build all possible transformations of each net
solutions = []
for net in nets:
    for composition in compositions:
        net_prime = composition(net)
        if net_prime not in solutions:
            solutions.append(net_prime)

def to_relative(m):
    # Find the coordinates of the 1s in the supplied matrix
    return tuple(sorted([(x, y) for y, row in enumerate(m) for x, v in enumerate(row) if v == 1]))

# All possible solutions using a relative coordinate system
relative_coordinate_solutions = set(to_relative(s) for s in solutions)

def fold_cube(number_list):
    # Convert from numbered cells to relative coordinates
    relative_coordinates = normalise(sorted([((n - 1) // 5, (n - 1) % 5) for n in number_list]))
    # See if we have a known solution
    return relative_coordinates in relative_coordinate_solutions

# number the cube faces like a dice 1-6
def folding(grid,face,list_on_face,remain_list):
    faces=[face]
    dirs=[1,-1,5,-5]
    if list_on_face % 5==1:
        dirs.remove(-1)
    if list_on_face % 5 ==0:
        dirs.remove(1)
    goto_dirs=[]
    print(remain_list)
    for direction in dirs:
        goto_cell=direction+list_on_face
        print((goto_cell,remain_list))
        if goto_cell in remain_list:
            remain_list.remove(goto_cell)
            goto_dirs.append(direction)
    print(("face",face,"grid",grid,"listCELL",list_on_face,"to",goto_dirs))
    for direction in goto_dirs:
        faces.extend(folding(
            grid=new_grid(face,direction,grid),
            face=new_face(grid, direction),
            list_on_face=list_on_face+direction,
            remain_list=remain_list
        ))
    return faces

def new_face(grid,direction):
    return grid[[1,-1,5,-5].index(direction)]

def new_grid(face, direction, grid):
    opposite_face={1:6,2:4,6:1,4:2,5:3,3:5}
    dir_index={1:0,-1:1,5:2,-5:3}
    newgrid=grid.copy()
    newgrid[dir_index[-direction]]=face
    newgrid[dir_index[direction]]=opposite_face[face]
    return newgrid

def fold_cube(number_list):
    faces=folding(grid=[3,5,2,4], #in dir [1,-1,5,-5]
            face=1,
            list_on_face=number_list[0],
            remain_list=number_list[1:])
    return sorted(faces)==list(range(1,7))
    #return True or False


BASE = {'  x\nxxx\n x \n x ', ' x\n x\nxx\nx \nx ', ' x \n xx\nxx \n x ', 'xx \n xx\n x \n x ', ' x \n xx\nxx \nx  ', 'xxx\n x \n x \n x ', 'xx \n x \n xx\n x ', 'xx \n x \n xx\n  x', 'xx \n x \n x \n xx', '  xx\n xx \nxx  ', ' x \nxxx\n x \n x '}

def reflect(s):
    return '\n'.join(r[::-1] for r in s.split('\n'))

def rotate(s):
    return '\n'.join(''.join(c[::-1]) for c in zip(*s.split('\n')))

def fold_cube(arr):
    table = [[' '] * 5 for _ in range(5)]
    x0, y0, x1, y1 = 4, 4, 0, 0
    for p in arr:
        x, y = divmod(p - 1, 5)
        table[x][y] = 'x'
        x0, y0 = min(x, x0), min(y, y0)
        x1, y1 = max(x, x1), max(y, y1)
    net = '\n'.join(''.join(r[y0:y1 + 1]) for r in table[x0:x1 + 1])
    net2 = reflect(net)
    for i in range(4):
        if net in BASE or net2 in BASE:
            return True
        net, net2 = rotate(net), rotate(net2)
    return False

db = [{9, 14, 18, 19, 20, 24}, {13, 14, 15, 16, 17, 18}, {2, 3, 8, 9, 10, 13}, {3, 6, 7, 8, 9, 12}, {4, 7, 8, 9, 10, 14}, {4, 6, 7, 8, 9, 13}, {8, 9, 13, 17, 18, 23}, {5, 10, 14, 15, 19, 24}, {3, 8, 12, 13, 18, 19}, {8, 12, 13, 14, 18, 23}, {7, 11, 12, 13, 17, 22}, {8, 13, 17, 18, 23, 24}, {8, 12, 13, 14, 18, 23}, {3, 8, 12, 13, 18, 19}, {13, 16, 17, 18, 19, 23}, {4, 9, 13, 14, 15, 19}, {2, 7, 8, 11, 12, 16}, {1, 6, 7, 8, 13, 14}, {13, 17, 18, 19, 20, 22}, {8, 12, 13, 14, 15, 18}, {15, 17, 18, 19, 20, 23}, {3, 6, 7, 8, 9, 12}, {8, 13, 17, 18, 19, 24}, {3, 4, 8, 13, 17, 18}, {6, 11, 12, 13, 14, 18}, {8, 12, 13, 18, 19, 24}, {2, 7, 8, 9, 10, 14}, {15, 17, 18, 19, 20, 25}, {10, 14, 15, 18, 19, 24}, {15, 17, 18, 19, 20, 24}, {11, 16, 17, 18, 19, 23}, {4, 8, 9, 14, 15, 19}, {5, 8, 9, 10, 14, 19}, {9, 13, 14, 19, 20, 24}, {8, 13, 14, 19, 20, 24}, {11, 16, 17, 18, 19, 22}, {2, 6, 7, 8, 13, 14}, {8, 12, 13, 18, 19, 23}, {8, 9, 14, 15, 19, 24}, {9, 13, 14, 19, 20, 24}, {3, 8, 12, 13, 14, 18}, {7, 12, 13, 17, 21, 22}, {7, 12, 16, 17, 18, 23}, {1, 2, 3, 7, 12, 17}, {9, 14, 18, 19, 24, 25}, {9, 11, 12, 13, 14, 16}, {14, 17, 18, 19, 20, 24}, {4, 9, 13, 14, 19, 20}, {2, 7, 8, 9, 14, 15}, {2, 3, 8, 9, 10, 13}, {6, 11, 12, 13, 17, 22}, {12, 16, 17, 18, 23, 24}, {7, 12, 16, 17, 18, 21}, {13, 14, 17, 18, 21, 22}, {4, 6, 7, 8, 9, 12}, {12, 17, 18, 19, 21, 22}, {8, 13, 18, 19, 22, 23}, {7, 12, 13, 18, 19, 23}, {1, 2, 7, 8, 9, 12}, {9, 14, 19, 20, 23, 24}, {11, 16, 17, 18, 19, 21}, {8, 13, 14, 17, 18, 23}, {9, 14, 15, 19, 23, 24}, {7, 12, 13, 14, 15, 19}, {6, 11, 12, 13, 17, 22}, {2, 6, 7, 8, 12, 17}, {3, 6, 7, 8, 9, 13}, {4, 5, 7, 8, 9, 12}, {3, 8, 9, 12, 13, 17}, {2, 3, 8, 9, 10, 15}, {6, 11, 12, 17, 22, 23}, {13, 17, 18, 19, 20, 24}, {8, 9, 14, 15, 19, 24}, {6, 7, 12, 13, 17, 22}, {9, 13, 14, 15, 17, 18}, {7, 12, 16, 17, 18, 22}, {8, 9, 14, 15, 19, 24}, {4, 9, 10, 14, 18, 19}, {8, 12, 13, 18, 19, 24}, {3, 4, 9, 10, 14, 19}, {8, 13, 14, 19, 24, 25}, {7, 12, 16, 17, 22, 23}, {8, 11, 12, 13, 17, 22}, {7, 12, 16, 17, 18, 22}, {2, 6, 7, 8, 13, 14}, {3, 4, 6, 7, 8, 12}, {3, 8, 9, 10, 14, 19}, {8, 13, 14, 15, 19, 24}, {4, 8, 9, 10, 12, 13}, {8, 13, 17, 18, 19, 23}, {2, 6, 7, 8, 9, 13}, {13, 16, 17, 18, 19, 23}, {3, 8, 12, 13, 14, 17}, {3, 7, 8, 9, 13, 18}, {9, 14, 15, 18, 19, 24}, {3, 6, 7, 8, 9, 12}, {11, 16, 17, 18, 19, 23}, {8, 9, 11, 12, 13, 18}, {7, 12, 13, 14, 15, 19}, {10, 12, 13, 14, 15, 18}, {8, 13, 17, 18, 23, 24}, {4, 9, 13, 14, 15, 19}, {13, 16, 17, 18, 23, 24}, {1, 2, 3, 7, 12, 17}, {9, 12, 13, 14, 15, 17}, {4, 9, 10, 14, 18, 19}, {12, 13, 18, 19, 24, 25}, {2, 7, 11, 12, 17, 18}, {2, 6, 7, 12, 13, 17}, {3, 7, 8, 9, 10, 13}, {1, 6, 7, 8, 9, 14}, {2, 7, 8, 9, 10, 12}, {3, 7, 8, 9, 10, 13}, {3, 7, 8, 13, 14, 19}, {5, 9, 10, 13, 14, 19}, {8, 13, 14, 17, 18, 23}, {4, 8, 9, 14, 15, 19}, {4, 8, 9, 14, 19, 20}, {3, 7, 8, 13, 14, 19}, {1, 2, 7, 8, 9, 12}, {7, 8, 13, 14, 19, 20}, {14, 15, 17, 18, 19, 23}, {8, 11, 12, 13, 17, 22}, {4, 8, 9, 12, 13, 18}, {4, 8, 9, 14, 19, 20}, {4, 9, 13, 14, 15, 18}, {7, 12, 13, 14, 15, 17}, {2, 7, 8, 11, 12, 16}, {1, 6, 7, 8, 9, 13}, {15, 17, 18, 19, 20, 24}, {8, 9, 10, 11, 12, 13}, {12, 16, 17, 18, 19, 21}, {9, 14, 18, 19, 20, 25}, {3, 4, 8, 12, 13, 18}, {9, 12, 13, 14, 15, 17}, {6, 7, 12, 13, 17, 22}, {10, 14, 15, 18, 19, 24}, {2, 3, 6, 7, 12, 17}, {4, 9, 13, 14, 15, 19}, {2, 7, 8, 9, 13, 18}, {3, 8, 13, 14, 17, 18}, {9, 10, 13, 14, 19, 24}, {3, 8, 9, 12, 13, 18}, {8, 12, 13, 14, 15, 18}, {14, 15, 18, 19, 22, 23}, {12, 16, 17, 18, 19, 21}, {7, 11, 12, 17, 18, 22}, {3, 6, 7, 8, 9, 11}, {7, 8, 12, 16, 17, 22}, {9, 13, 14, 15, 17, 18}, {6, 11, 12, 13, 14, 17}, {13, 17, 18, 19, 21, 22}, {12, 16, 17, 18, 19, 24}, {6, 11, 12, 13, 14, 18}, {14, 16, 17, 18, 19, 23}, {3, 7, 8, 9, 10, 13}, {4, 6, 7, 8, 9, 13}, {9, 12, 13, 14, 15, 20}, {2, 3, 6, 7, 12, 17}, {12, 13, 18, 19, 20, 23}, {8, 9, 10, 11, 12, 13}, {8, 13, 14, 17, 18, 23}, {4, 6, 7, 8, 9, 14}, {9, 12, 13, 14, 15, 19}, {3, 7, 8, 9, 11, 12}, {3, 8, 9, 12, 13, 18}, {9, 14, 15, 18, 19, 23}, {8, 12, 13, 14, 15, 18}, {3, 4, 9, 14, 15, 19}, {4, 8, 9, 14, 15, 20}, {7, 11, 12, 13, 14, 18}, {4, 9, 14, 18, 19, 20}, {2, 6, 7, 12, 13, 18}, {13, 16, 17, 18, 19, 23}, {9, 14, 19, 23, 24, 25}, {6, 11, 12, 13, 14, 18}, {7, 12, 16, 17, 18, 23}, {10, 12, 13, 14, 15, 19}, {4, 8, 9, 10, 14, 19}, {13, 16, 17, 18, 19, 22}, {4, 7, 8, 9, 10, 13}, {14, 17, 18, 19, 20, 23}, {12, 17, 18, 19, 20, 23}, {16, 17, 18, 23, 24, 25}, {8, 11, 12, 13, 14, 18}, {3, 8, 12, 13, 14, 18}, {9, 12, 13, 14, 15, 18}, {9, 13, 14, 19, 20, 25}, {7, 12, 13, 14, 15, 20}, {11, 12, 13, 18, 19, 20}, {7, 8, 13, 14, 18, 23}, {3, 6, 7, 8, 9, 12}, {10, 13, 14, 15, 17, 18}, {6, 7, 12, 13, 17, 22}, {4, 7, 8, 9, 10, 15}, {10, 13, 14, 15, 19, 24}, {8, 9, 11, 12, 13, 16}, {8, 12, 13, 14, 15, 20}, {11, 16, 17, 18, 19, 24}, {2, 7, 8, 9, 10, 12}, {13, 16, 17, 18, 19, 23}, {9, 12, 13, 14, 15, 19}, {4, 6, 7, 8, 9, 12}, {12, 16, 17, 18, 19, 22}, {2, 7, 11, 12, 13, 18}, {14, 15, 17, 18, 19, 22}, {3, 7, 8, 13, 18, 19}, {9, 11, 12, 13, 14, 16}, {9, 10, 12, 13, 14, 17}, {8, 12, 13, 14, 15, 18}, {12, 17, 18, 19, 20, 25}, {4, 7, 8, 9, 10, 14}, {8, 13, 18, 19, 22, 23}, {7, 8, 13, 14, 15, 20}, {12, 16, 17, 18, 19, 23}, {4, 7, 8, 9, 10, 15}, {1, 2, 7, 12, 17, 18}, {3, 7, 8, 13, 14, 18}, {4, 8, 9, 14, 15, 20}, {8, 12, 13, 14, 16, 17}, {4, 7, 8, 9, 10, 14}, {6, 7, 12, 13, 14, 19}, {12, 17, 18, 19, 20, 23}, {14, 17, 18, 19, 20, 23}, {8, 12, 13, 18, 19, 24}, {8, 12, 13, 18, 23, 24}, {8, 12, 13, 14, 15, 19}, {4, 5, 8, 9, 12, 13}, {6, 11, 12, 13, 17, 22}, {7, 11, 12, 13, 14, 19}, {8, 13, 17, 18, 19, 24}, {13, 16, 17, 18, 19, 23}, {2, 6, 7, 8, 12, 17}, {7, 12, 16, 17, 18, 22}, {14, 17, 18, 19, 20, 23}, {4, 8, 9, 10, 14, 19}, {6, 11, 12, 13, 14, 18}, {5, 8, 9, 10, 14, 19}, {9, 14, 18, 19, 20, 24}, {9, 13, 14, 15, 17, 18}, {6, 11, 12, 17, 18, 22}, {7, 11, 12, 17, 22, 23}, {7, 11, 12, 13, 14, 18}, {10, 13, 14, 15, 19, 24}, {12, 13, 18, 19, 20, 23}, {13, 16, 17, 18, 23, 24}, {8, 13, 17, 18, 19, 22}, {12, 16, 17, 18, 19, 23}, {11, 16, 17, 18, 19, 22}, {13, 16, 17, 18, 19, 23}, {14, 15, 17, 18, 19, 23}, {14, 17, 18, 19, 20, 24}, {6, 7, 12, 17, 22, 23}, {8, 11, 12, 13, 18, 19}, {8, 9, 11, 12, 13, 18}, {8, 13, 18, 19, 22, 23}, {18, 19, 20, 21, 22, 23}, {7, 12, 13, 14, 16, 17}, {7, 12, 16, 17, 18, 22}, {3, 4, 6, 7, 8, 13}, {2, 7, 8, 9, 10, 14}, {3, 8, 12, 13, 18, 19}, {9, 11, 12, 13, 14, 17}, {9, 13, 14, 17, 18, 23}, {7, 11, 12, 13, 18, 19}, {9, 11, 12, 13, 14, 16}, {10, 12, 13, 14, 15, 20}, {2, 7, 11, 12, 13, 16}, {14, 17, 18, 19, 20, 22}, {8, 12, 13, 14, 15, 18}, {15, 17, 18, 19, 20, 24}, {13, 17, 18, 19, 24, 25}, {12, 17, 18, 19, 20, 22}, {4, 5, 7, 8, 9, 14}, {9, 14, 18, 19, 24, 25}, {2, 7, 8, 11, 12, 17}, {12, 17, 18, 19, 20, 23}, {3, 8, 9, 13, 17, 18}, {6, 7, 12, 13, 14, 19}, {9, 14, 15, 18, 19, 24}, {10, 14, 15, 18, 19, 23}, {7, 12, 16, 17, 18, 22}, {12, 16, 17, 18, 19, 22}, {15, 17, 18, 19, 20, 22}, {3, 8, 13, 14, 17, 18}, {4, 8, 9, 12, 13, 18}, {7, 11, 12, 13, 18, 19}, {8, 11, 12, 13, 14, 17}, {7, 12, 16, 17, 18, 22}, {14, 17, 18, 19, 21, 22}, {1, 2, 7, 8, 13, 14}, {12, 16, 17, 18, 19, 23}, {2, 6, 7, 8, 9, 12}, {7, 11, 12, 13, 14, 19}, {5, 7, 8, 9, 10, 12}, {5, 7, 8, 9, 10, 15}, {4, 7, 8, 9, 14, 15}, {9, 12, 13, 14, 19, 20}, {3, 4, 9, 14, 15, 19}, {8, 13, 14, 17, 18, 23}, {2, 7, 8, 11, 12, 17}, {2, 7, 8, 13, 18, 19}, {8, 12, 13, 14, 18, 23}, {5, 10, 14, 15, 19, 24}, {4, 7, 8, 9, 10, 15}, {14, 15, 17, 18, 19, 23}, {8, 9, 14, 15, 19, 24}, {3, 7, 8, 9, 10, 13}, {9, 14, 15, 18, 19, 24}, {5, 7, 8, 9, 10, 15}, {7, 12, 17, 21, 22, 23}, {3, 8, 9, 14, 15, 20}, {12, 13, 18, 19, 20, 24}, {8, 13, 17, 18, 19, 23}, {7, 12, 13, 14, 16, 17}, {9, 13, 14, 19, 24, 25}, {8, 12, 13, 14, 16, 17}, {4, 9, 13, 14, 15, 18}, {1, 2, 7, 8, 12, 17}, {8, 9, 11, 12, 13, 16}, {9, 14, 18, 19, 20, 23}, {12, 17, 18, 19, 20, 24}, {3, 8, 12, 13, 17, 22}, {9, 14, 19, 20, 23, 24}, {2, 7, 8, 13, 14, 18}, {9, 14, 19, 23, 24, 25}, {8, 9, 11, 12, 13, 18}, {2, 7, 11, 12, 13, 17}, {1, 6, 11, 12, 17, 22}, {1, 6, 7, 12, 13, 17}, {7, 8, 12, 17, 21, 22}, {14, 16, 17, 18, 19, 24}, {12, 16, 17, 18, 19, 22}, {7, 12, 13, 16, 17, 22}, {8, 13, 14, 17, 18, 23}, {9, 13, 14, 19, 20, 24}, {2, 7, 8, 12, 16, 17}, {3, 4, 6, 7, 8, 13}, {4, 5, 7, 8, 9, 14}, {2, 6, 7, 8, 9, 13}, {7, 12, 16, 17, 22, 23}, {2, 6, 7, 8, 12, 17}, {7, 11, 12, 17, 18, 22}, {9, 10, 14, 18, 19, 23}, {8, 9, 13, 17, 18, 23}, {14, 16, 17, 18, 19, 23}, {7, 8, 12, 17, 21, 22}, {14, 16, 17, 18, 19, 23}, {10, 12, 13, 14, 15, 18}, {3, 6, 7, 8, 9, 12}, {6, 11, 12, 13, 17, 22}, {8, 11, 12, 13, 17, 22}, {8, 12, 13, 14, 15, 19}, {8, 9, 11, 12, 13, 16}, {7, 12, 16, 17, 18, 21}, {8, 11, 12, 13, 17, 22}, {8, 13, 14, 19, 24, 25}, {3, 8, 9, 10, 12, 13}, {6, 11, 12, 13, 17, 22}, {7, 12, 13, 14, 15, 17}, {14, 15, 17, 18, 19, 23}, {4, 9, 10, 13, 14, 18}, {8, 9, 10, 11, 12, 13}, {2, 7, 8, 13, 18, 19}, {3, 8, 12, 13, 18, 19}, {9, 13, 14, 19, 20, 24}, {2, 7, 12, 13, 18, 23}, {6, 11, 12, 17, 18, 23}, {4, 9, 14, 18, 19, 20}, {9, 12, 13, 14, 15, 17}, {6, 11, 12, 13, 14, 16}, {9, 13, 14, 15, 19, 24}, {14, 17, 18, 19, 20, 25}, {14, 17, 18, 19, 21, 22}, {7, 12, 13, 18, 19, 23}, {3, 4, 9, 14, 15, 20}, {2, 6, 7, 8, 9, 13}, {3, 4, 6, 7, 8, 13}, {8, 13, 14, 15, 19, 24}, {13, 16, 17, 18, 19, 22}, {8, 9, 11, 12, 13, 17}, {2, 6, 7, 12, 13, 17}, {1, 2, 7, 8, 9, 12}, {3, 8, 9, 12, 13, 17}, {2, 3, 8, 9, 10, 14}, {13, 16, 17, 18, 19, 23}, {8, 11, 12, 13, 14, 17}, {2, 7, 8, 11, 12, 16}, {6, 7, 12, 13, 14, 17}, {13, 18, 19, 20, 22, 23}, {10, 12, 13, 14, 15, 20}, {1, 6, 11, 12, 17, 22}, {2, 7, 8, 11, 12, 17}, {8, 9, 14, 19, 20, 25}, {2, 7, 11, 12, 17, 18}, {2, 6, 7, 12, 17, 18}, {8, 13, 18, 19, 22, 23}, {4, 8, 9, 10, 14, 19}, {9, 14, 18, 19, 20, 23}, {4, 6, 7, 8, 9, 12}, {14, 17, 18, 19, 20, 24}, {4, 8, 9, 10, 14, 19}, {2, 6, 7, 8, 9, 12}, {13, 17, 18, 19, 20, 25}, {7, 11, 12, 17, 22, 23}, {3, 6, 7, 8, 9, 13}, {7, 11, 12, 13, 14, 17}, {7, 11, 12, 13, 14, 19}, {4, 9, 13, 14, 18, 23}, {7, 12, 13, 16, 17, 22}, {4, 8, 9, 12, 13, 17}, {11, 12, 17, 18, 19, 23}, {12, 16, 17, 18, 19, 23}, {3, 8, 12, 13, 14, 19}, {1, 2, 3, 7, 12, 17}, {8, 9, 11, 12, 13, 18}, {7, 12, 13, 16, 17, 21}, {7, 11, 12, 13, 17, 22}, {2, 7, 11, 12, 13, 18}, {12, 16, 17, 18, 19, 23}, {7, 11, 12, 17, 18, 22}, {3, 8, 9, 10, 12, 13}, {7, 8, 9, 13, 18, 23}, {2, 7, 8, 9, 13, 18}, {11, 12, 13, 18, 19, 20}, {7, 12, 13, 14, 15, 20}, {4, 7, 8, 9, 10, 12}, {2, 7, 11, 12, 13, 17}, {9, 11, 12, 13, 14, 18}, {9, 11, 12, 13, 14, 17}, {9, 11, 12, 13, 14, 19}, {3, 8, 12, 13, 18, 19}, {4, 8, 9, 13, 17, 18}, {12, 13, 18, 19, 24, 25}, {7, 11, 12, 13, 17, 22}, {13, 18, 19, 20, 22, 23}, {6, 11, 12, 13, 17, 22}, {4, 5, 7, 8, 9, 13}, {8, 9, 10, 11, 12, 13}, {8, 9, 11, 12, 13, 17}, {8, 9, 10, 14, 19, 24}, {9, 14, 19, 23, 24, 25}, {13, 17, 18, 19, 20, 23}, {9, 14, 18, 19, 20, 24}, {11, 12, 17, 18, 19, 23}, {13, 16, 17, 18, 19, 23}, {7, 12, 16, 17, 18, 21}, {7, 8, 9, 13, 18, 23}, {5, 8, 9, 10, 12, 13}, {5, 7, 8, 9, 10, 12}, {2, 6, 7, 12, 13, 17}, {9, 12, 13, 14, 15, 19}, {14, 16, 17, 18, 19, 23}, {7, 12, 17, 18, 21, 22}, {16, 17, 18, 23, 24, 25}, {8, 12, 13, 14, 15, 17}, {8, 13, 17, 18, 19, 23}, {2, 3, 8, 9, 10, 14}, {4, 9, 14, 15, 18, 19}, {15, 17, 18, 19, 20, 25}, {4, 9, 10, 14, 18, 19}, {13, 16, 17, 18, 19, 23}, {7, 12, 13, 16, 17, 21}, {9, 11, 12, 13, 14, 19}, {3, 7, 8, 9, 10, 14}, {4, 8, 9, 14, 15, 19}, {2, 7, 12, 13, 16, 17}, {6, 7, 8, 13, 14, 15}, {7, 8, 12, 16, 17, 22}, {8, 9, 14, 15, 19, 24}, {3, 8, 13, 14, 17, 18}, {7, 12, 13, 14, 15, 19}, {15, 17, 18, 19, 20, 24}, {3, 6, 7, 8, 9, 11}, {3, 7, 8, 11, 12, 16}, {8, 12, 13, 16, 17, 22}, {6, 7, 8, 12, 17, 22}, {11, 16, 17, 18, 19, 21}, {8, 13, 14, 15, 19, 24}, {2, 6, 7, 12, 17, 18}, {7, 8, 13, 18, 23, 24}, {3, 6, 7, 8, 9, 13}, {4, 7, 8, 9, 13, 18}, {13, 17, 18, 19, 20, 24}, {3, 7, 8, 9, 10, 12}, {1, 6, 7, 12, 13, 17}, {3, 8, 9, 14, 15, 19}, {13, 16, 17, 18, 19, 22}, {12, 16, 17, 18, 19, 22}, {3, 7, 8, 9, 10, 14}, {14, 17, 18, 19, 20, 22}, {7, 11, 12, 17, 18, 22}, {1, 6, 7, 12, 13, 17}, {1, 6, 7, 8, 12, 17}, {2, 6, 7, 12, 17, 18}, {3, 7, 8, 9, 10, 15}, {7, 12, 13, 16, 17, 21}, {4, 6, 7, 8, 9, 13}, {1, 6, 7, 8, 9, 11}, {2, 7, 11, 12, 13, 17}, {13, 17, 18, 19, 20, 24}, {9, 13, 14, 15, 19, 24}, {8, 9, 13, 17, 18, 23}, {4, 9, 10, 13, 14, 19}, {1, 2, 3, 7, 12, 17},{9, 10, 14, 18, 19, 24}, {7, 12, 13, 14, 15, 18}, {4, 5, 9, 14, 18, 19}, {3, 4, 7, 8, 11, 12}, {8, 13, 18, 22, 23, 24}, {1, 6, 7, 12, 17, 18}, {2, 7, 8, 9, 10, 13}, {3, 6, 7, 8, 9, 14}, {8, 9, 14, 19, 20, 24}, {4, 6, 7, 8, 9, 11}, {9, 12, 13, 14, 18, 23}, {1, 2, 7, 8, 9, 13}, {8, 9, 13, 18, 22, 23}, {2, 3, 8, 13, 14, 18}, {1, 2, 7, 12, 13, 18}, {14, 16, 17, 18, 19, 22}, {1, 2, 7, 12, 13, 17}, {6, 7, 12, 13, 14, 18}, {3, 6, 7, 8, 12, 17}, {3, 7, 8, 11, 12, 17}, {1, 6, 7, 12, 13, 18}, {8, 12, 13, 17, 21, 22}, {4, 5, 8, 9, 14, 19}, {1, 6, 7, 8, 9, 12}, {7, 8, 11, 12, 17, 22}, {13, 16, 17, 18, 19, 21}, {6, 7, 12, 17, 18, 23}, {8, 11, 12, 13, 14, 19}, {2, 7, 11, 12, 16, 21}, {5, 9, 10, 14, 18, 19}, {4, 9, 14, 15, 20, 25}, {7, 8, 12, 16, 17, 21}, {9, 10, 12, 13, 14, 18}, {9, 10, 12, 13, 14, 19}, {2, 3, 7, 11, 12, 17}, {9, 13, 14, 17, 18, 22}, {7, 11, 12, 17, 18, 23}, {13, 14, 16, 17, 18, 23}, {14, 17, 18, 19, 24, 25}, {8, 13, 14, 15, 17, 18}, {7, 11, 12, 13, 14, 16}, {3, 8, 13, 14, 19, 24}, {2, 3, 8, 9, 13, 18}, {3, 4, 7, 8, 13, 18}, {4, 5, 9, 13, 14, 18}, {4, 9, 13, 14, 15, 20}, {2, 3, 8, 13, 14, 19}, {2, 7, 12, 16, 17, 18}, {10, 14, 15, 19, 23, 24}, {5, 7, 8, 9, 10, 14}, {3, 4, 6, 7, 8, 11}, {2, 7, 8, 13, 14, 19}, {3, 6, 7, 8, 13, 14}, {7, 12, 13, 14, 18, 23}, {7, 12, 13, 18, 23, 24}, {2, 3, 8, 13, 18, 19}, {14, 16, 17, 18, 19, 21}, {11, 12, 17, 18, 19, 22}, {8, 9, 12, 13, 18, 23}, {3, 4, 5, 6, 7, 8}, {4, 5, 9, 13, 14, 19}, {8, 9, 12, 13, 16, 17}, {8, 11, 12, 13, 14, 16}, {2, 7, 8, 9, 10, 15}, {4, 7, 8, 9, 11, 12}, {5, 9, 10, 13, 14, 18}, {13, 16, 17, 18, 19, 24}, {14, 18, 19, 20, 22, 23}, {8, 12, 13, 14, 19, 20}, {3, 8, 13, 17, 18, 19}, {3, 4, 9, 14, 19, 20}, {14, 15, 17, 18, 19, 24},{7, 8, 13, 18, 19, 23}, {2, 3, 8, 9, 14, 15}, {6, 7, 12, 17, 18, 22}, {3, 4, 8, 12, 13, 17}, {13, 14, 16, 17, 18, 21}, {2, 3, 7, 12, 16, 17}, {2, 6, 7, 8, 9, 14}, {13, 14, 16, 17, 18, 22}, {2, 3, 7, 11, 12, 16}, {2, 7, 8, 9, 11, 12}, {3, 4, 5, 9, 14, 19}, {8, 9, 13, 17, 18, 22}, {10, 12, 13, 14, 15, 17}, {7, 8, 13, 14, 15, 18}, {7, 12, 13, 14, 19, 20}, {2, 6, 7, 8, 9, 11}, {7, 8, 13, 18, 19, 24}, {3, 7, 8, 9, 14, 15}, {11, 16, 17, 18, 23, 24}, {7, 8, 13, 14, 15, 19}, {2, 3, 4, 8, 13, 18}, {8, 13, 14, 17, 18, 22}, {7, 12, 13, 18, 19, 24}, {9, 13, 14, 18, 22, 23}, {9, 10, 13, 14, 17, 18},{9, 10, 14, 19, 23, 24}, {12, 13, 18, 19, 20, 25}, {6, 11, 12, 13, 14, 19}, {8, 13, 14, 18, 22, 23}, {3, 7, 8, 12, 16, 17}, {1, 2, 7, 8, 9, 14}, {8, 12, 13, 16, 17, 21}, {6, 7, 12, 13, 18, 19}, {9, 12, 13, 14, 16, 17}, {15, 18, 19, 20, 22, 23}, {5, 7, 8, 9, 10, 13}, {6, 11, 12, 13, 18, 19}, {8, 13, 14, 19, 20, 25}, {8, 9, 14, 19, 24, 25}, {12, 17, 18, 19, 24, 25}, {1, 2, 3, 8, 9, 10}, {11, 12, 17, 18, 19, 24}, {3, 8, 9, 14, 19, 20}, {11, 12, 17, 18, 23, 24}]
def fold_cube(num_list): return set(num_list) in db
'''
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿ wait - that's illegal ⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⡏⠉⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿
⣿⣿⣿⣿⣿⣿⠀⠀⠀⠈⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⠉⠁⠀⣿
⣿⣿⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠙⠿⠿⠿⠻⠿⠿⠟⠿⠛⠉⠀⠀⠀⠀⠀⣸⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣷⣄⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⣴⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⢰⣹⡆⠀⠀⠀⠀⠀⠀⣭⣷⠀⠀⠀⠸⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠈⠉⠀⠀⠤⠄⠀⠀⠀⠉⠁⠀⠀⠀⠀⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⢾⣿⣷⠀⠀⠀⠀⡠⠤⢄⠀⠀⠀⠠⣿⣿⣷⠀⢸⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡀⠉⠀⠀⠀⠀⠀⢄⠀⢀⠀⠀⠀⠀⠉⠉⠁⠀⠀⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿
'''

U = 1
F = 2
R = 3


def fold_cube(num_list):
    print(num_list)
    nums = set(num_list)
    faces = set()
    print(nums)
    try:
        face = Face(nums.pop(), nums, faces)
    except:
        return False
    faces.add(face)
#     print(face.val)
#     print(face.borders())
#     for face in faces:
#         print(face.val, face.z)
#     return sum(f.z for f in faces) == 0
    fs = set(f.z for f in faces)
    return fs == {1, 2, 3, -1, -2, -3}


class Face:
    def __init__(self, val, nums, faces, x=F, y=R, z=U):
#         print(val, nums)
        self.nums = nums
        self.val = val
        self.z = z
        self.x = x
        self.y = y

        for num in self.borders():
            self.nums.remove(num)
            z, x, y = self.fold(num)
#             print(z, x, y)
            faces.add(Face(num, self.nums, faces, x=x, y=y, z=z))

    def borders(self):
        borders = []
        ds = (self.val - 1) // 5
        ms = (self.val - 1) % 5
        for num in self.nums:
            dn = (num - 1) // 5
            mn = (num - 1) % 5
#             print(ds, ms, dn, mn)
            if ds - dn == 0 and abs(ms - mn) == 1 or ms - mn == 0 and abs(ds - dn) == 1:
                borders.append(num)
#             if abs(self.val - num) in [1, 5]:
#                 borders.append(num)
#         print(borders)
        return borders

    def fold(self, num):
        relation = self.val - num
        if relation == 5:
            z = self.x
            x = -self.z
            y = self.y
        elif relation == -5:
            z = -self.x
            x = self.z
            y = self.y
        elif relation == 1:
            z = -self.y
            x = self.x
            y = self.z
        elif relation == -1:
            z = self.y
            x = self.x
            y = -self.z
        return z, x, y

    def __hash__(self):
        return self.val

    def __eq__(self, other):
        return self.val == other.val


def fold_cube(number_list):
    print(number_list)
    connections = 0
    number_list.sort()

    for i in range(len(number_list)):
        if ((number_list[i] - 1) in number_list) and (number_list[i] not in [6, 11, 16, 21]): connections += 1
    for j in range(len(number_list)):
        if ((number_list[j] - 5) in number_list): connections += 1

    # in cube 6 faces are connected by 5 edges
    if connections != 5: return False

    horizontal = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]
    vertical = [[1, 6, 11, 16, 21], [2, 7, 12, 17, 22], [3, 8, 13, 18, 23], [4, 9, 14, 19, 24], [5, 10, 15, 20, 25]]

    lenghts_horizontal = []
    height = 0

    for h in horizontal:
        lh = 1
        any_in_row = False
        for i in range(len(number_list)):
            if number_list[i] in h:
                any_in_row = True
            if ((number_list[i] - 1) in number_list) and (number_list[i] in h) and (
                    number_list[i] not in [6, 11, 16, 21]):
                lh += 1
        if lh > 1:
            lenghts_horizontal.append(lh)
        elif any_in_row:
            lenghts_horizontal.append(1)
        if any_in_row:
            height += 1

    if lenghts_horizontal[0] == 4 or lenghts_horizontal[-1] == 4:
        return False

    is_3_horizontal_fist = False
    if lenghts_horizontal[0] == 3 or lenghts_horizontal[-1] == 3:
        is_3_horizontal_fist = True

    lenghts_horizontal.sort(reverse=True)

    if lenghts_horizontal[0] > 4: return False

    width = 0
    lenghts_vertical = []

    for v in vertical:
        lv = 1
        any_in_row = False
        for i in range(len(number_list)):
            if number_list[i] in v:
                any_in_row = True
            if ((number_list[i] - 5) in number_list) and (number_list[i] in v):
                lv += 1
        if lv > 1:
            lenghts_vertical.append(lv)
        elif any_in_row:
            lenghts_vertical.append(1)
        if any_in_row:
            width += 1

    if height == 3 and width == 3:
        return False

    if lenghts_vertical[0] == 4 or lenghts_vertical[-1] == 4:
        return False

    is_3_vertical_first = False
    if (lenghts_vertical[0] == 3) or (lenghts_vertical[-1] == 3):
        is_3_vertical_first = True

    lenghts_vertical.sort(reverse=True)

    if is_3_vertical_first and height == 4:
        return False

    if is_3_horizontal_fist and width == 4:
        return False

    if lenghts_vertical[0] > 4: return False

    if (lenghts_vertical[0] == 3 and lenghts_vertical[1] == 3 and width == 2):
        return True

    if (lenghts_horizontal[0] == 3 and lenghts_horizontal[1] == 3 and height == 2):
        return True

    upper = False
    upper_list = [1, 2, 3, 4, 5]
    lower = False
    lower_list = [21, 22, 23, 24, 25]
    left = False
    left_list = [1, 6, 11, 16, 21]
    right = False
    right_list = [5, 10, 15, 20, 25]

    for n in number_list:
        if n in upper_list:
            upper = True
        if n in lower_list:
            lower = True
        if n in left_list:
            left = True
        if n in right_list:
            right = True

    if upper and lower: return False
    if right and left: return False

    if lenghts_vertical == [2, 2, 1, 1] and lenghts_horizontal == [3, 2, 1] and width == 3 and height == 4:
        return False

    if ((lenghts_vertical[0] == 3 and width == 4 and height != 2) or (
            lenghts_horizontal[0] == 3 and height == 4 and width != 2)) and (lenghts_vertical != [3, 1, 1, 1]):
        return True

    if (lenghts_vertical[0] == 4 and width == 3 and height != 2) or (
            lenghts_horizontal[0] == 4 and height == 3 and width != 2):
        return True

    if (lenghts_vertical[0] == 3 and height == 4 and width != 2) or (
            lenghts_horizontal[0] == 3 and width == 4 and height != 2):
        return True

    if (lenghts_vertical[0] == 4 and height == 3 and width != 2) or (
            lenghts_horizontal[0] == 4 and width == 3 and height != 2):
        return True

    if lenghts_vertical == [2, 2, 2] and lenghts_horizontal == [2, 2, 1, 1]:
        return True

    if lenghts_horizontal == [2, 2, 2] and lenghts_vertical == [2, 2, 1, 1]:
        return True

    return False

def fold_cube(number_list):
    print(number_list)
    connections = 0
    number_list.sort()

    for i in range(len(number_list)):
        if ((number_list[i] - 1) in number_list) and (number_list[i] not in [6, 11, 16, 21]): connections += 1
    for j in range(len(number_list)):
        if ((number_list[j] - 5) in number_list): connections += 1

    # in cube 6 faces are connected by 5 edges
    if connections != 5: return False

    horizontal = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]
    vertical = [[1, 6, 11, 16, 21], [2, 7, 12, 17, 22], [3, 8, 13, 18, 23], [4, 9, 14, 19, 24], [5, 10, 15, 20, 25]]

    lenghts_horizontal = []
    height = 0

    for h in horizontal:
        lh = 1
        any_in_row = False
        for i in range(len(number_list)):
            if number_list[i] in h:
                any_in_row = True
            if ((number_list[i] - 1) in number_list) and (number_list[i] in h) and (
                    number_list[i] not in [6, 11, 16, 21]):
                lh += 1
        if lh > 1:
            lenghts_horizontal.append(lh)
        elif any_in_row:
            lenghts_horizontal.append(1)
        if any_in_row:
            height += 1

    if lenghts_horizontal[0] == 4 or lenghts_horizontal[-1] == 4:
        return False

    # print(lenghts_horizontal)
    is_3_horizontal_fist = False
    if lenghts_horizontal[0] == 3 or lenghts_horizontal[-1] == 3:
        is_3_horizontal_fist = True

    lenghts_horizontal.sort(reverse=True)

    if lenghts_horizontal[0] > 4: return False

    width = 0
    lenghts_vertical = []

    for v in vertical:
        lv = 1
        any_in_row = False
        for i in range(len(number_list)):
            if number_list[i] in v:
                any_in_row = True
            if ((number_list[i] - 5) in number_list) and (number_list[i] in v):
                lv += 1
        if lv > 1:
            lenghts_vertical.append(lv)
        elif any_in_row:
            lenghts_vertical.append(1)
        if any_in_row:
            width += 1

    #    print(lenghts_vertical)
    #    print(lenghts_horizontal)
    #    print(height,width)

    if height == 3 and width == 3:
        return False

    if lenghts_vertical[0] == 4 or lenghts_vertical[-1] == 4:
        return False

    is_3_vertical_first = False
    if (lenghts_vertical[0] == 3) or (lenghts_vertical[-1] == 3):
        is_3_vertical_first = True
    # print(height)

    #    print(lenghts_vertical)
    #    print(lenghts_vertical[0])

    lenghts_vertical.sort(reverse=True)

    if is_3_vertical_first and height == 4:
        return False

    if is_3_horizontal_fist and width == 4:
        return False

    print((height, width))
    print(lenghts_vertical)
    print(lenghts_horizontal)

    # print(is_3_vertical_first, is_3_horizontal_fist)

    if lenghts_vertical[0] > 4: return False

    # if (lenghts_vertical[0] == 3 and width == 3) or (lenghts_horizontal[0] == 3 and height == 3):
    #    return True

    # if (lenghts_vertical[0] == 3 and height == 3) or (lenghts_horizontal[0] == 3 and width == 3):
    #    return True

    if (lenghts_vertical[0] == 3 and lenghts_vertical[1] == 3 and width == 2):
        return True

    if (lenghts_horizontal[0] == 3 and lenghts_horizontal[1] == 3 and height == 2):
        return True

    upper = False
    upper_list = [1, 2, 3, 4, 5]
    lower = False
    lower_list = [21, 22, 23, 24, 25]
    left = False
    left_list = [1, 6, 11, 16, 21]
    right = False
    right_list = [5, 10, 15, 20, 25]

    for n in number_list:
        if n in upper_list:
            upper = True
        if n in lower_list:
            lower = True
        if n in left_list:
            left = True
        if n in right_list:
            right = True

    if upper and lower: return False
    if right and left: return False

    if lenghts_vertical == [2, 2, 1, 1] and lenghts_horizontal == [3, 2, 1] and width == 3 and height == 4:
        return False

    if ((lenghts_vertical[0] == 3 and width == 4 and height != 2) or (
            lenghts_horizontal[0] == 3 and height == 4 and width != 2)) and (lenghts_vertical != [3, 1, 1, 1]):
        return True

    if (lenghts_vertical[0] == 4 and width == 3 and height != 2) or (
            lenghts_horizontal[0] == 4 and height == 3 and width != 2):
        return True

    if (lenghts_vertical[0] == 3 and height == 4 and width != 2) or (
            lenghts_horizontal[0] == 3 and width == 4 and height != 2):
        return True

    if (lenghts_vertical[0] == 4 and height == 3 and width != 2) or (
            lenghts_horizontal[0] == 4 and width == 3 and height != 2):
        return True

    if lenghts_vertical == [2, 2, 2] and lenghts_horizontal == [2, 2, 1, 1]:
        return True

    if lenghts_horizontal == [2, 2, 2] and lenghts_vertical == [2, 2, 1, 1]:
        return True

    return False