Diana loves playing with numbers. She's got n cards with positive integer numbers a_i written on them. She spends her free time multiplying the numbers on the cards. She picks a non-empty subset of the cards and multiplies all the numbers a_i written on them.

Diana is happy when the product of the numbers ends with her favorite digit d. Now she is curious what cards she should pick so that the product of the numbers on them is the largest possible and the last decimal digit of the product is d. Please, help her.

Input

The first line contains the integers n and d (1≤ n≤ 10^5, 0≤ d≤ 9). The second line contains n integers a_i (1≤ a_i≤ 1000). 

Output

On the first line, print the number of chosen cards k (1≤ k≤ n). On the next line, print the numbers written on the chosen cards in any order. 

If it is impossible to choose a subset of cards with the product that ends with the digit d, print the single line with -1.

Examples

Input


6 4
4 11 8 2 1 13


Output


5
1 2 4 11 13 


Input


3 1
2 4 6


Output


-1


Input


5 7
1 3 1 5 3


Output


-1


Input


6 3
8 9 4 17 11 5


Output


3
9 11 17 


Input


5 6
2 2 2 2 2


Output


4
2 2 2 2 

Note

In the first example, 1 × 2 × 4 × 11 × 13 = 1144, which is the largest product that ends with the digit 4. The same set of cards without the number 1 is also a valid answer, as well as a set of 8, 11, and 13 with or without 1 that also has the product of 1144.

In the second example, all the numbers on the cards are even and their product cannot end with an odd digit 1.

In the third example, the only possible products are 1, 3, 5, 9, 15, and 45, none of which end with the digit 7.

In the fourth example, 9 × 11 × 17 = 1683, which ends with the digit 3. 

In the fifth example, 2 × 2 × 2 × 2 = 16, which ends with the digit 6.

Source: codeforces | Difficulty: HARD
Tags: Number theory

Dynamic programming

Mathematics


Solutions:
import math
(n, d) = map(int, input().split())
a = [int(_) for _ in input().split()]
dp = [[-10000000000.0] * 10 for _ in range(n + 1)]
come = [[(0, 0, 0) for i in range(10)] for j in range(n + 1)]
dp[0][1] = 0
for i in range(n):
	for j in range(10):
		val = dp[i][j] + math.log(a[i])
		if dp[i + 1][j * a[i] % 10] <= val:
			dp[i + 1][j * a[i] % 10] = val
			come[i + 1][j * a[i] % 10] = (i, j, 1)
		val = dp[i][j]
		if dp[i + 1][j] <= val:
			dp[i + 1][j] = val
			come[i + 1][j] = (i, j, 0)
now = n
prod = 1
dig = d
soln = []
while now > 0:
	if come[now][dig][2] == 1:
		soln.append(a[now - 1])
	(now, dig) = come[now][dig][:2]
for i in soln:
	prod = prod * i % 10
if prod == d and len(soln) > 0:
	print(len(soln))
	print(' '.join(map(str, soln)))
else:
	print(-1)


from sys import stdin, stdout
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import *
from random import *
input = stdin.readline
prin = stdout.write
from random import sample
from collections import Counter, deque
from fractions import *
from math import sqrt, ceil, log2, gcd
mod = 10 ** 9 + 7

def f(arr, i, j, d, dist):
	if i == j:
		return
	nn = max(arr[i:j])
	for tl in range(i, j):
		if arr[tl] == nn:
			dist[tl] = d
			f(arr, i, tl, d + 1, dist)
			f(arr, tl + 1, j, d + 1, dist)

def ps(n):
	cp = 0
	lk = 0
	arr = {}
	while n % 2 == 0:
		n = n // 2
		lk += 1
	for ps in range(3, ceil(sqrt(n)) + 1, 2):
		lk = 0
		cc = 0
		while n % ps == 0:
			n = n // ps
			lk += 1
	if n != 1:
		lk += 1
		return False
	return True

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def factorials(n, r):
	slr = n
	dpr = r
	qlr = 1
	qs = 1
	mod = 10 ** 9 + 7
	for ip in range(n - r + 1, n + 1):
		qlr = qlr * ip % mod
	for ij in range(1, r + 1):
		qs = qs * ij % mod
	ans = qlr * modInverse(qs) % mod
	return ans

def modInverse(b):
	qr = 10 ** 9 + 7
	return pow(b, qr - 2, qr)
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def power(arr):
	listrep = arr
	subsets = []
	for i in range(2 ** len(listrep)):
		subset = []
		for k in range(len(listrep)):
			if i & 1 << k:
				subset.append(listrep[k])
		subsets.append(subset)
	return subsets

def pda(n):
	list = []
	for i in range(1, int(sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				list.append(i)
			else:
				list.append(n // i)
				list.append(i)
	return list

def dis(xa, ya, xb, yb):
	return sqrt((xa - xb) ** 2 + (ya - yb) ** 2)

def ii():
	return int(input())

def ilist():
	return [int(x) for x in input().strip().split()]

def islist():
	return list(map(str, input().split().rstrip()))

def inp():
	return input().strip()

def google(test, ans):
	return 'Case #' + str(test) + ': ' + str(ans)

def overlap(x1, y1, x2, y2):
	if x2 > y1:
		return y1 - x2
	if y1 > y2:
		return y2 - x2
	return y1 - x2

def pal(s):
	k = len(s)
	n = len(s) // 2
	for i in range(n):
		if s[i] == s[k - 1 - i]:
			continue
		else:
			return 0
	return 1
t = 1
for p in range(t):
	(n, kll) = ilist()
	arr = [-1]
	arr.extend(ilist())
	dp = [[0 for i in range(10)] for j in range(n + 1)]
	pointer = [[[-1 for k in range(3)] for i in range(10)] for j in range(n + 1)]
	pp = []
	for i in range(1, n + 1):
		for j in range(10):
			last = arr[i] % 10
			for k in range(10):
				fs = 1 if dp[i - 1][k] == 0 else k
				if fs * last % 10 == j:
					if dp[i][j] < dp[i - 1][k] + log2(arr[i] + 0.01):
						pointer[i][j][0] = 0
						pointer[i][j][1] = i - 1
						pointer[i][j][2] = k
						dp[i][j] = max(dp[i - 1][k] + log2(arr[i] + 0.01), dp[i][j])
			if dp[i][j] < dp[i - 1][j]:
				pointer[i][j][0] = 1
				pointer[i][j][1] = i - 1
				pointer[i][j][2] = j
				dp[i][j] = max(dp[i - 1][j], dp[i][j])
	ans = dp[n][kll]
	if ans == 0:
		print(-1)
		continue
	ap = n
	jp = kll
	count = 0
	while True:
		up = ap
		if pointer[ap][jp][0] != 0:
			ap = pointer[up][jp][1]
			jp = pointer[up][jp][2]
		elif pointer[ap][jp][0] == 0:
			pp.append(arr[ap])
			count += 1
			ap = pointer[up][jp][1]
			jp = pointer[up][jp][2]
		if ap <= 0:
			break
	print(count)
	print(*pp)


[n, d] = [int(x) for x in input().split(' ')]
a = [int(x) for x in input().split(' ')]
if d % 2 != 0:
	a = [x for x in a if x % 2 != 0]
if d % 5 != 0:
	a = [x for x in a if x % 5 != 0]
n = len(a)
f = [[-1] * 10]
sel = [[-1] * 10]
f[0][1] = 1
for i in range(1, 1 + n):
	sel.append([0] * 10)
	f.append([-1] * 10)
	for j in range(10):
		v = f[i - 1][j]
		if v == -1:
			continue
		t = a[i - 1] * j % 10
		if f[i][t] == -1 or f[i][t] > v:
			f[i][t] = v
			sel[i][t] = (1, j)
		v *= a[i - 1]
		if (f[i][j] == -1 or f[i][j] > v) and v < 2e+18:
			f[i][j] = v
			sel[i][j] = (0, j)
if f[n][d] == -1:
	print(-1)
	exit()
ans = []
for i in range(n, 0, -1):
	(u, x) = sel[i][d]
	if u == 1:
		ans.append(a[i - 1])
	d = x
if len(ans) == 0:
	print(-1)
	exit()
print(len(ans))
print(' '.join([str(x) for x in ans]))


import math
import sys
import argparse

def main():
	input = sys.stdin.readline
	(n, d) = list(map(int, input().split()))
	a = list(map(float, input().split()))
	b = []
	for i in range(0, n):
		b.append(int(a[i]))
		a[i] = math.log(a[i])
	mlog = sum(a)
	f = [[0.0 for j in range(0, 11)] for i in range(0, n)]
	c = [[[0, 0] for j in range(0, 11)] for i in range(0, n)]
	ans = []
	for i in range(n):
		if i != 0:
			for j in range(10):
				f[i][j] = f[i - 1][j]
				c[i][j][0] = c[i - 1][j][0]
				c[i][j][1] = c[i - 1][j][1]
			for j in range(10):
				if f[i - 1][j] + a[i] > f[i][j * b[i] % 10]:
					f[i][j * b[i] % 10] = f[i - 1][j] + a[i]
					c[i][j * b[i] % 10][0] = i
					c[i][j * b[i] % 10][1] = j
			if f[i][b[i] % 10] < a[i]:
				f[i][b[i] % 10] = a[i]
				c[i][b[i] % 10][0] = i
				c[i][b[i] % 10][1] = -1
			continue
		if i == 0:
			for j in range(10):
				f[i][j] = math.log(0.0001) - mlog
				c[i][j][0] = -1
				c[i][j][1] = -1
			f[i][b[0] % 10] = a[0]
			c[i][b[0] % 10][0] = 0
			c[i][b[0] % 10][1] = -1
			continue
	if f[n - 1][d] <= 0:
		print(-1)
		return 0
	(x, y) = (c[n - 1][d][0], c[n - 1][d][1])
	while y != -1:
		ans.append(b[x])
		u = x - 1
		if u < 0:
			break
		x = c[u][y][0]
		y = c[u][y][1]
	if x >= 0:
		ans.append(b[x])
	print(len(ans))
	print(' '.join(list(map(str, ans))))
if 0:
	print('test1000')
main()


import time
import os, sys
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import bisect_left, bisect_right
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string, heapq as h
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def getInt():
	return int(input())

def getStrs():
	return input().split()

def getInts():
	return list(map(int, input().split()))

def getStr():
	return input()

def listStr():
	return list(input())

def getMat(n):
	return [getInts() for _ in range(n)]

def getBin():
	return list(map(int, list(input())))

def isInt(s):
	return '0' <= s[0] <= '9'

def ceil_(a, b):
	return a // b + (a % b > 0)
MOD = 10 ** 9 + 7
from random import randint as ri

def solve():
	(N, D) = getInts()
	A = getInts()
	dec = [[] for _ in range(10)]
	for a in A:
		dec[a % 10].append(a)
	for i in range(10):
		dec[i].sort()
	if D == 0:
		if not dec[0] and (not (dec[2] and dec[5])):
			print(-1)
			return
		else:
			print(N)
			print(*A)
			return
	else:
		dec[0] = []
	if D == 5:
		if not dec[5]:
			print(-1)
			return
		else:
			ans = []
			for a in A:
				if a % 2:
					ans.append(a)
			print(len(ans))
			print(*ans)
			return
	else:
		dec[5] = []
	ans = []
	while dec[1]:
		ans.append(dec[1].pop())
	while len(dec[3]) > 6:
		for _ in range(4):
			ans.append(dec[3].pop())
	while len(dec[7]) > 6:
		for _ in range(4):
			ans.append(dec[7].pop())
	while len(dec[9]) > 2:
		for _ in range(2):
			ans.append(dec[9].pop())
	if D % 2:
		A = []
		for i in range(1, 10, 2):
			while dec[i]:
				A.append(dec[i].pop())
		best = -1
		best_mask = -1
		for x in range(1, pow(2, len(A))):
			mask = bin(x)[2:].zfill(len(A))
			tmp = 1
			for j in range(len(A)):
				if mask[j] == '1':
					tmp *= A[j]
			if tmp % 10 == D and tmp > best:
				best = tmp
				best_mask = mask
		if best != -1:
			for j in range(len(A)):
				if best_mask[j] == '1':
					ans.append(A[j])
			print(len(ans))
			print(*ans)
			return
		elif D == 1 and ans:
			print(len(ans))
			print(*ans)
			return
		else:
			print(-1)
			return
	while dec[6]:
		ans.append(dec[6].pop())
	while len(dec[2]) > 6:
		for _ in range(4):
			ans.append(dec[2].pop())
	while len(dec[8]) > 6:
		for _ in range(4):
			ans.append(dec[8].pop())
	while len(dec[4]) > 2:
		for _ in range(2):
			ans.append(dec[4].pop())
	A_odd = []
	A_even = []
	for i in range(0, 10, 2):
		while dec[i]:
			A_even.append(dec[i].pop())
		while dec[i + 1]:
			A_odd.append(dec[i + 1].pop())
	best = [-1] * 10
	best_mask = [-1] * 10
	if ans:
		best[1] = 1
		best_mask[1] = 0
	for x in range(1, pow(2, len(A_odd))):
		mask = bin(x)[2:].zfill(len(A_odd))
		tmp = 1
		for j in range(len(A_odd)):
			if mask[j] == '1':
				tmp *= A_odd[j]
		if tmp > best[tmp % 10]:
			best[tmp % 10] = tmp
			best_mask[tmp % 10] = x
	for x in range(1, pow(2, len(A_even))):
		mask = bin(x)[2:].zfill(len(A_even))
		tmp = 1
		for j in range(len(A_even)):
			if mask[j] == '1':
				tmp *= A_even[j]
		if tmp > best[tmp % 10]:
			best[tmp % 10] = tmp
			best_mask[tmp % 10] = x
	best_combo = -1
	best_pair = [-1, -1]
	for i in range(0, 10, 2):
		for j in range(1, 10, 2):
			if best[i] != -1 and best[j] != -1:
				curr = best[i] * best[j]
				if curr % 10 == D and curr > best_combo:
					best_combo = curr
					best_pair = [i, j]
	if best[D] > best_combo:
		x = best_mask[D]
		mask = bin(x)[2:].zfill(len(A_even))
		for j in range(len(A_even)):
			if mask[j] == '1':
				ans.append(A_even[j])
		print(len(ans))
		print(*ans)
		return
	if best_pair == [-1, -1]:
		print(-1)
		return
	(k, i) = best_pair
	x = best_mask[i]
	mask = bin(x)[2:].zfill(len(A_odd))
	for j in range(len(A_odd)):
		if mask[j] == '1':
			ans.append(A_odd[j])
	x = best_mask[k]
	mask = bin(x)[2:].zfill(len(A_even))
	for j in range(len(A_even)):
		if mask[j] == '1':
			ans.append(A_even[j])
	print(len(ans))
	print(*ans)
	return
solve()


import math

def main():
	(n, d) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	if d != 5 and d != 0:
		a = list(filter(lambda x: x % 5 != 0, a))
	if d & 1:
		a = list(filter(lambda x: x % 2, a))
	n = len(a)
	dp = [[-1000000.0 for _ in range(10)] for _ in range(n + 1)]
	path = [[(0, 0, 0) for _ in range(10)] for _ in range(n + 1)]
	dp[0][1] = 0
	for i in range(n):
		for j in range(10):
			val = dp[i][j] + math.log(a[i])
			if dp[i + 1][j * a[i] % 10] <= val:
				dp[i + 1][j * a[i] % 10] = val
				path[i + 1][j * a[i] % 10] = (i, j, 1)
			val = dp[i][j]
			if dp[i + 1][j] <= val:
				dp[i + 1][j] = val
				path[i + 1][j] = (i, j, 0)
	ans = []
	(test, pd) = (1, d)
	if dp[n][d] < 0:
		print(-1)
		return
	while n > 0:
		if path[n][d][2] == 1:
			ans.append(a[n - 1])
			test = test * a[n - 1] % 10
		(n, d) = path[n][d][:2]
	if len(ans) > 0:
		print(len(ans))
		print(' '.join(map(str, ans)))
	else:
		print(-1)
main()


import os, sys
from io import BytesIO, IOBase
from math import log10

def main():
	log = [0] + [log10(i) for i in range(1, 1001)]
	unit = [i % 10 for i in range(1001)]
	(n, d) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[0] * 10 for _ in range(n + 1)]
	ne = [[i for i in range(10)] for _ in range(n + 1)]
	take = [[0] * 10 for _ in range(n + 1)]
	for i in range(n):
		(dp[i + 1], uni) = (dp[i][:], unit[a[i]])
		for j in range(10):
			if not dp[i][j]:
				continue
			(xx, un) = (dp[i][j] + log[a[i]], unit[j * uni])
			if xx > dp[i + 1][un]:
				(dp[i + 1][un], ne[i + 1][un], take[i + 1][un]) = (xx, j, 1)
		if log[a[i]] > dp[i + 1][uni]:
			(dp[i + 1][uni], take[i + 1][uni], ne[i + 1][uni]) = (log[a[i]], 1, -1)
	(ind, ans) = (d, [])
	for i in range(n, 0, -1):
		if take[i][ind]:
			ans.append(a[i - 1])
		ind = ne[i][ind]
		if ind == -1:
			break
	if len(ans):
		print(len(ans))
		print(*ans)
	elif d == 1 and a.count(1):
		print(1)
		print(1)
	else:
		print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
(n, d) = map(int, input().split())
arr = [int(_) for _ in input().split()]
dp = [[-1000000.0 for _ in range(0, 10)] for _ in range(n + 1)]
path = [[(0, 0, 0) for _ in range(0, 10)] for _ in range(n + 1)]
dp[0][1] = 0
for i in range(n):
	for j in range(10):
		val = dp[i][j] + math.log(arr[i])
		if dp[i + 1][j * arr[i] % 10] <= val:
			dp[i + 1][j * arr[i] % 10] = val
			path[i + 1][j * arr[i] % 10] = (i, j, 1)
		val = dp[i][j]
		if dp[i + 1][j] <= val:
			dp[i + 1][j] = val
			path[i + 1][j] = (i, j, 0)
ans = []
(test, pd) = (1, d)
while n > 0:
	if path[n][d][2] == 1:
		ans.append(arr[n - 1])
		test = test * arr[n - 1] % 10
	(n, d) = path[n][d][:2]
if test == pd and len(ans) > 0:
	print(len(ans))
	print(' '.join(map(str, ans)))
else:
	print(-1)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt, log2
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def modInverse(b):
	g = gcd(b, mod)
	if g != 1:
		return -1
	else:
		return pow(b, mod - 2, mod)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def getdigit(a):
	return a % 10

def solve(case):
	(n, d) = sep()
	a = lis()
	dp = [[-1] * 10 for i in range(n)]
	dp[0][getdigit(a[0])] = log2(a[0])
	for i in range(1, n):
		for j in range(10):
			dp[i][j] = dp[i - 1][j]
		dp[i][getdigit(a[i])] = max(dp[i][getdigit(a[i])], log2(a[i]))
		for j in range(10):
			if dp[i - 1][j] != -1:
				dp[i][getdigit(a[i] * j)] = max(dp[i - 1][j] + log2(a[i]), dp[i][getdigit(a[i] * j)])
	if dp[n - 1][d] < 0:
		print(-1)
		return
	ans = []
	curr = d
	for i in range(n - 1, -1, -1):
		if i == 0:
			if dp[0][curr] != 0:
				ans.append(a[i])
			continue
		if dp[i][curr] != dp[i - 1][curr]:
			ans.append(a[i])
			find = dp[i][curr] - log2(a[i])
			if find == 0:
				break
			for j in range(10):
				if dp[i - 1][j] + log2(a[i]) == dp[i][curr]:
					curr = j
					break
	print(len(ans))
	print(' '.join((str(i) for i in ans)))
testcase(1)


import collections
import string
import math
import copy
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
mo = int(1000000000.0 + 7)

def exgcd(a, b):
	if not b:
		return (1, 0)
	(y, x) = exgcd(b, a % b)
	y -= a // b * x
	return (x, y)

def getinv(a, m):
	(x, y) = exgcd(a, m)
	return -1 if x == 1 else x % m

def comb(n, b):
	res = 1
	b = min(b, n - b)
	for i in range(b):
		res = res * (n - i) * getinv(i + 1, mo) % mo
	return res % mo

def quickpower(a, n):
	res = 1
	while n:
		if n & 1:
			res = res * a % mo
		n >>= 1
		a = a * a % mo
	return res

def dis(a, b):
	return abs(a[0] - b[0]) + abs(a[1] - b[1])

def getpref(x):
	if x > 1:
		return x * (x - 1) >> 1
	else:
		return 0

def orafli(upp):
	primes = []
	marked = [False for i in range(upp + 3)]
	prvs = [i for i in range(upp + 3)]
	for i in range(2, upp):
		if not marked[i]:
			primes.append(i)
		for j in primes:
			if i * j >= upp:
				break
			marked[i * j] = True
			prvs[i * j] = j
			if i % j == 0:
				break
	return (primes, prvs)

def lower_ord(c: str) -> int:
	return ord(c) - 97

def upper_ord(c: str) -> int:
	return ord(c) - 65

def read_list():
	return [int(i) for i in input().split()]

def read_int():
	s = input().split()
	if len(s) == 1:
		return int(s[0])
	else:
		return map(int, s)

def ask(s):
	print(f'? {s}', flush=True)

def answer(s):
	print(f'{s}', flush=True)
import random

def solve():
	(n, d) = read_int()
	l = read_list()
	dp = {}
	pv = []
	for i in l:
		cd = copy.deepcopy(dp)
		cp = {}
		for (k, v) in dp.items():
			lg = math.log(i, 2)
			if v + lg > cd.get(k * i % 10, 0):
				cp[v + lg] = v
				cd[k * i % 10] = v + lg
		lg = math.log(i, 2)
		if lg > cd.get(i % 10, 0):
			cp[lg] = 0
			cd[i % 10] = lg
		pv.append(cp)
		dp = cd
	sel = []
	if d not in dp:
		print(-1)
	else:
		c = dp[d]
		while pv:
			fac = pv.pop()
			curfac = l.pop()
			if c in fac:
				sel.append(curfac)
				c = fac[c]
		print(len(sel))
		print(*sel)
solve()


(n, d) = [int(s) for s in input().split(' ')]
a = [int(s) for s in input().split(' ')]
aDig = [[] for _ in range(10)]
for x in a:
	aDig[x % 10].append(x)
for ad in aDig:
	ad.sort()
c0 = len(aDig[0])
c5 = len(aDig[5])
c1379 = len(aDig[1]) + len(aDig[3]) + len(aDig[7]) + len(aDig[9])
c2468 = len(aDig[2]) + len(aDig[4]) + len(aDig[6]) + len(aDig[8])

def solveMod5(r3, r7, r9, m5):
	(prod, prodList) = (0, [])
	for a in range(len(r3) + 1):
		for b in range(len(r7) + 1):
			for c in range(len(r9) + 1):
				currList = []
				if a:
					currList += r3[-a:]
				if b:
					currList += r7[-b:]
				if c:
					currList += r9[-c:]
				r = 1
				for t in currList:
					r *= t
				if r % 5 == m5 and r > prod:
					prod = r
					prodList = currList
	return (prod, prodList)
if d == 0:
	if c0 or (c2468 and c5):
		print(n)
		print(*sorted(a))
	else:
		print(-1)
elif d == 5:
	if c5:
		print(c5 + c1379)
		print(*sorted((x for x in a if x & 1)))
	else:
		print(-1)
else:
	usedSurely = aDig[1]
	if d % 2 == 0 and (len(aDig[2]) >= 7 or len(aDig[4]) >= 3 or len(aDig[6]) >= 1 or (len(aDig[8]) >= 7)):
		for ev in [2, 4, 6, 8]:
			aDig[(ev + 5) % 10] += aDig[ev]
			aDig[(ev + 5) % 10].sort()
			aDig[ev] = []
		d = (d + 5) % 10
	for t in [3, 7]:
		while len(aDig[t]) >= 7:
			usedSurely += aDig[t][-4:]
			for _ in range(4):
				aDig[t].pop()
	while len(aDig[9]) >= 3:
		usedSurely += aDig[9][-2:]
		for _ in range(2):
			aDig[9].pop()
	if d & 1:
		d5 = d % 5
		(prod, prodList) = solveMod5(aDig[3], aDig[7], aDig[9], d5)
		if prod and len(prodList) + len(usedSurely):
			print(len(prodList) + len(usedSurely))
			print(*sorted(prodList + usedSurely))
		else:
			print(-1)
	else:
		d5 = d % 5
		(prod, prodList) = (0, [])
		for ev in [2, 4, 6, 8]:
			if not len(aDig[ev]):
				continue
			save = aDig[ev][-1]
			aDig[ev].pop()
			dd5 = d5 * pow(save, -1, 5) % 5
			(currProd, currProdList) = solveMod5(sorted(aDig[3] + aDig[8]), sorted(aDig[7] + aDig[2]), sorted(aDig[9] + aDig[4]), dd5)
			currProd *= save
			currProdList.append(save)
			if currProd > prod:
				prod = currProd
				prodList = currProdList
			aDig[ev].append(save)
		if prod and len(prodList) + len(usedSurely):
			print(len(prodList) + len(usedSurely))
			print(*sorted(prodList + usedSurely))
		else:
			print(-1)


from sys import stdin, stdout
import heapq
from collections import defaultdict
import math
import bisect

def main():
	(n, d) = list(map(int, stdin.readline().split()))
	arr = list(map(int, stdin.readline().split()))
	dp = [[-10000000000.0 for _ in range(10)] for _ in range(n + 1)]
	jump = [[(0, 0, 0) for _ in range(10)] for _ in range(n + 1)]
	dp[0][1] = 0
	for (i, x) in enumerate(arr):
		for j in range(10):
			digi = x * j % 10
			v = dp[i][j] + math.log(x)
			if dp[i + 1][digi] <= v:
				dp[i + 1][digi] = v
				jump[i + 1][digi] = (i, j, 1)
			v = dp[i][j]
			if dp[i + 1][j] <= v:
				dp[i + 1][j] = v
				jump[i + 1][j] = (i, j, 0)
	rec = n
	ans = []
	dig = d
	while rec > 0:
		if jump[rec][dig][2] == 1:
			ans.append(arr[jump[rec][dig][0]])
		(rec, dig) = jump[rec][dig][:2]
	sol = 1
	for x in ans:
		sol = sol * x % 10
	if sol == d and len(ans) > 0:
		stdout.write(str(len(ans)) + '\n')
		stdout.write(' '.join([str(x) for x in ans]))
	else:
		stdout.write('-1')
main()


line1 = input().split(' ')
line2 = input().split(' ')
(n, d) = (int(line1[0]), int(line1[1]))
s = [0]
for i in line2:
	s.append(int(i))
(mx, w) = ([], [])
now = 1
for i in range(2):
	mx.append([0] * 10)
	w.append([1] * 10)
(pren, pred) = ([], [])
for i in range(n + 1):
	pren.append([0] * 10)
	pred.append([0] * 10)
mx[now][1] = 1
w[now][1] = 1
for i in range(n):
	for j in range(10):
		mx[~now][j] = mx[now][j]
		w[~now][j] = w[now][j]
		pren[i + 1][j] = pren[i][j]
		pred[i + 1][j] = pred[i][j]
	for j in range(10):
		to = j * s[i + 1] % 10
		nv = mx[now][j] * s[i + 1]
		nw = w[now][j] + len(str(nv)) - min(w[now][j], 70)
		nv = int(str(nv)[:70])
		if w[~now][to] < nw or (w[~now][to] == nw and mx[~now][to] < nv):
			mx[~now][to] = nv
			w[~now][to] = nw
			pren[i + 1][to] = i + 1
			pred[i + 1][to] = j
	now = ~now
if pren[n][d]:
	ans = []
	(nown, nowd) = (n, d)
	while pren[nown][nowd] > 0:
		(nown, nowd) = (pren[nown][nowd] - 1, pred[nown][nowd])
		ans.append(s[nown + 1])
	print(len(ans))
	for i in ans:
		print(i, end=' ')
	print('')
else:
	print(-1)


import math
import random
import sys
import time
seed = int(time.time())
random.seed(seed)

def equalsf(a, b):
	return abs(a - b) < 1e-06

def solve(N, D, A):
	lgA = [math.log(a) for a in A]
	dp = [[None for _ in range(10)] for _ in range(N)]
	parents = [[-1 for _ in range(10)] for _ in range(N)]
	for i in range(N):
		d = A[i] % 10
		dp[i][d] = lgA[i]
		if i == 0:
			continue
		for prev_d in range(10):
			if dp[i][prev_d] is None or (dp[i - 1][prev_d] is not None and dp[i][prev_d] < dp[i - 1][prev_d]):
				dp[i][prev_d] = dp[i - 1][prev_d]
				parents[i][prev_d] = prev_d
		for prev_d in range(10):
			if dp[i - 1][prev_d] is None:
				continue
			prod_d = d * prev_d % 10
			cand = dp[i - 1][prev_d] + lgA[i]
			if dp[i][prod_d] is None or dp[i][prod_d] < cand:
				dp[i][prod_d] = cand
				parents[i][prod_d] = prev_d
	ans = []
	parent = D
	for i in range(N - 1, -1, -1):
		if dp[i][parent] is None:
			break
		prev_p = parents[i][parent]
		if i > 0 and prev_p >= 0:
			print(i, 'cur:', parent, 'prev:', prev_p, file=sys.stderr)
			if dp[i - 1][prev_p] is not None and equalsf(dp[i - 1][prev_p] + lgA[i], dp[i][parent]):
				ans.append(A[i])
			parent = prev_p
		else:
			ans.append(A[i])
			break
	return ans

def main():
	(N, D) = [int(x) for x in input().strip().split()]
	A = [int(x) for x in input().strip().split()]
	ans = solve(N, D, A)
	if len(ans) == 0:
		print(-1)
	else:
		print(len(ans))
		print(' '.join([str(x) for x in ans]))
if '__main__' == __name__:
	main()


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
(n, k) = mp()
arr = lmp()
dp = l2d(n + 1, 10, -1)
for i in range(1, n + 1):
	for j in range(10):
		dp[i][j] = max(dp[i][j], dp[i - 1][j])
		if j == arr[i - 1] % 10:
			dp[i][j] = max(dp[i][j], round(log(arr[i - 1]), 6))
		if dp[i - 1][j] != -1:
			dp[i][j * arr[i - 1] % 10] = max(dp[i][j * arr[i - 1] % 10], round(dp[i - 1][j] + log(arr[i - 1]), 6))
ansl = []
j = k
flg = True
for i in range(n, 0, -1):
	if dp[i][j] == -1:
		flg = False
		print(-1)
		break
	if dp[i][j] == dp[i - 1][j]:
		continue
	elif dp[i][j] == round(log(arr[i - 1]), 6):
		ansl.append(arr[i - 1])
		break
	else:
		ansl.append(arr[i - 1])
		j = dp[i - 1].index(round(dp[i][j] - log(arr[i - 1]), 6))
if flg:
	print(len(ansl))
	print(*ansl)