Absent-minded Masha got set of n cubes for her birthday.

At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural x such she can make using her new cubes all integers from 1 to x.

To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number.

The number can't contain leading zeros. It's not required to use all cubes to build a number.

Pay attention: Masha can't make digit 6 from digit 9 and vice-versa using cube rotations.

Input

In first line integer n is given (1 ≤ n ≤ 3) — the number of cubes, Masha got for her birthday.

Each of next n lines contains 6 integers aij (0 ≤ aij ≤ 9) — number on j-th face of i-th cube.

Output

Print single integer — maximum number x such Masha can make any integers from 1 to x using her cubes or 0 if Masha can't make even 1.

Examples
input
3
0 1 2 3 4 5
6 7 8 9 0 1
2 3 4 5 6 7
output
87
input
3
0 1 3 5 6 8
1 2 4 5 7 8
2 3 4 6 7 9
output
98
Note

In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.

题目大意:最多有三个六面体,每个六面体有6个数字,可以用任意个六面体组成数字,但是一次每个六面体只能用一次,问你能组成最大的连续数字是多少?例如样例1:可以组成1,2,3,4,。。87

题解: 因为最大只有三个六面体,最多有999种情况,dfs遍历每种情况,存入v【值】= 1,否则默认 v【值】 = 0;最后从1到1000遍历v数组,查找第一个断点则是答案。

 1 /* ***********************************************
 2 Author        :wsw
 3 Created Time  :2017/11/7 17:27:24
 4 TASK          :cf_R444_B.cpp
 5 LANG          :C++
 6 ************************************************ */
 7 #include <iostream>
 8 #include <cstdio>
 9 #include <cstring>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <cmath>
17 #include <cstdlib>
18 #include <ctime>
19 using namespace std;
20 int n;
21 int num[3][6];
22 int value[1000];//存值
23 int vis[3];//记录是用过
24 void dfs(int ant,int no){
25     value[no] = 1;
26     if(ant>n) return ;
27     for(int i = 0;i<n;i++){
28         if(vis[i]) continue;
29         for(int j = 0;j<6;j++){
30             vis[i] = 1;
31             dfs(ant+1,no*10+num[i][j]);
32             vis[i] = 0;
33         }
34     }
35 }
36 int main()
37 {
38     cin >> n;
39     for(int i = 0;i<n;i++){
40         for(int j = 0; j < 6;j++){
41             cin >> num[i][j];
42         }
43     }
44     dfs(0,0);
45     for(int i = 1;i<1000;i++){
46         if(!value[i]){
47             cout << i-1 << endl;
48             break;
49         }
50     }
51     return 0;
52 }

人生路也许有很多条,希望你能走完属于你的那一条。

posted on 2017-11-07 17:55  沫米沫  阅读(346)  评论(0编辑  收藏  举报