#!/usr/bin/env python3

import sys

squares = int(sys.argv[1])
sum = int(sys.argv[2])
exclude = set()
include = set()
for x in sys.argv[3:]:
    if x[0] == '+':
        include.update(int(d) for d in x[1:])
    else:
        exclude.update(int(d) for d in x)


def solve(start, squares, sum):
    if squares == 1:
        if start <= sum and 0 < sum < 10 and not sum in exclude:
            return [[sum]]
        return []
    sums = []
    for i in range(start, 10):
        if i in exclude:
            continue
        sub = solve(i + 1, squares - 1, sum - i)
        if len(sub) > 0:
            for s in sub:
                solution = [i, ] + s
                sums.append(solution)
    return sums


def all_included(solution):
    return include.issubset(solution)


solutions = filter(all_included, solve(1, squares, sum))
shared = set()


def set_str(s):
    return ''.join(map(lambda i: '%s ' % i if i in set(s) else '. ', range(1, 10)))


for i, s in enumerate(solutions):
    print(set_str(s))
    if i == 0:
        shared = set(s)
    else:
        shared &= set(s)
shared.discard(include)
if len(shared) > 0:
    print(set_str(shared), 'shared')
print('\n' + '\n'.join([''.join(map(str, x)) for x in solutions]))
