from typing import List, Tuple, Union
import numpy as np


def reshape_matrix(a: List[List[Union[int, float]]], new_shape: Tuple[int, int]) -> List[List[Union[int, float]]]:
    m = len(a)
    n = len(a[0])
    num = m*n
    out = -1
    lista = []
    
    k=0
    if num == new_shape[0]*new_shape[1]:
        out = [[0]*new_shape[1] for _ in range(new_shape[0])]
        for i in range(m):
            for j in range(n):
                lista.append(a[i][j])
        for i in range(new_shape[0]):
            for j in range(new_shape[1]):
                out[i][j] = lista[k]
                k+=1
    return out



def main():
    try:
        a = eval(input())
        new_shape = eval(input())
        result = reshape_matrix(a, new_shape)
        print(result)
    except Exception as e:
        print(f"输入格式错误: {e}")

if __name__ == "__main__":
    main()