#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];
  }
  if (n == 1) {
    cout << c[0] << endl;
  } else if (n == 2) {
    int k = -1;
    int d = -1;
    for (int i = 0; i < (int)(m); i++) {
      if (s[i] == 1) {
        k = i;
      } else if (s[i] == 2) {
        d = i;
      }
    }
    if (c[k] != 0 && d != -1) {
      cout << c[k] << c[d] << endl;
    } else if (c[k] != 0 && d == -1) {
      cout << c[k] << "0" << endl;
    } else if (c[k] == 0) {
      cout << "-1" << endl;
    }
  } else {
    int v = -1;
    int t = -1;
    int u = -1;
    for (int i = 0; i < (int)(m); i++) {
      if (s[i] == 1) {
        v = i;
      } else if (s[i] == 2) {
        t = i;
      } else if (s[i] == 3) {
        u = i;
      }
    }
    if (c[v] != 0 && t != -1 && u != -1) {
      cout << c[v] << c[t] << c[u] << endl;
    } else if (c[v] != 0 && t != -1 && u == -1) {
      cout << c[v] << c[t] << "0" << endl;
    } else if (c[v] != 0 && t == -1 && u == -1) {
      cout << c[v] << "00" << endl;
    } else if (c[v] != 0 && t == -1 && u != -1) {
      cout << c[v] << "0" << c[u] << endl;
    } else if (c[v] == 0) {
      cout << "-1" << endl;
    }
  }
  return 0;
}
