#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int s[6], c[6];
  for (int i = 0; i < (int)(6); i++) {
    cin >> s[i] >> c[i];
  }
  for (int i = 0; i < (int)(m); i++) {
    for (int j = i + 1; j < m; j++) {
      if (s[i] == s[i + j] && c[i] == c[i + j]) {
        cout << "-1" << endl;
      }
    }
  }
  if (n == 1) {
    cout << c[0] << endl;
  } else if (n == 2) {
    for (int i = 0; i < (int)(m); i++) {
      if ((s[i] == 1 && c[i] == 0)) {
        cout << "-1" << endl;
      }
      int k = 0;
      int d = 0;
      if (s[i] == 1 && c[i] != 0) {
        k = i;
      } else if (s[i] == 2) {
        d = i;
      }
      cout << s[k] << s[d] << endl;
    }
  }
  return 0;
}
