#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * f;
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    int a = 0, b = 0, c = 0;
    int vis[10] = {0};
    vector<int> edges[10];
    while (m--) {
      int x, y;
      cin >> x >> y;
      if (x != 1 && y == 0) continue;
      edges[x].push_back(y);
    }
    for (int i = 1; i <= 3; i++) sort(edges[i].begin(), edges[i].end());
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < edges[i].size(); j++) {
        if (vis[edges[i][j]] == 0) {
          if (i == 1)
            a = edges[i][j];
          else if (i == 2)
            b = edges[i][j];
          else
            c = edges[i][j];
          vis[edges[i][j]] = 1;
          break;
        }
      }
    }
    if (n == 1)
      cout << a << endl;
    else if (n == 2) {
      if (a != 0)
        cout << a << b << endl;
      else
        cout << -1 << endl;
    } else {
      if (a != 0 && b != c)
        cout << a << b << c << endl;
      else
        cout << -1 << endl;
    }
  }
  return 0;
}
