#P6380. Lucky Number

    ID: 19596 Type: Default 1000ms 256MiB

Lucky Number

Lucky Number

Before leaving this world, Man-Yu Li wants to find her own lucky number. She provides an integer \(a\) and a binary string \(S\) of length \(b\) (with characters '0' and '1').

A lucky number is defined as a positive integer \(n\) that satisfies both of the following conditions:

  • \(n\) has exactly \(b\) digits and does not have any leading zero.
  • For every \(1 \le i \le b\), if the \(i\)th character of \(S\) is '1', the number formed by the first \(i\) digits of \(n\) is a multiple of \(a\); otherwise (if the \(i\)th character is '0'), the prefix of \(n\) is not a multiple of \(a\).

For example, if \(n=312311\), then its first 3 digits form the number 312, and its first 5 digits form the number 31231.

Your task is to help Man-Yu Li determine the smallest lucky number \(n\) satisfying the above conditions. If no such number exists, output -1.

inputFormat

The input consists of two lines. The first line contains a single integer (a). The second line contains a binary string (S) representing the conditions for the prefixes.

outputFormat

Output the smallest lucky number that meets the conditions. If no such number exists, output (-1).

sample

2
101
210