#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 = INF, b = INF, c = INF;
    int vis[10] = {0};
    while (m--) {
      int x, y;
      cin >> x >> y;
      if (vis[y]) continue;
      if (x == 1) {
        if (a > y) {
          a = y;
          vis[y] = 1;
        }
      } else if (x == 2) {
        if (b > y) {
          b = y;
          vis[y] = 1;
        }
      } else {
        if (c > y) {
          c = y;
          vis[y] = 1;
        }
      }
    }
    if (n == 1) {
      if (a != INF && b == INF && c == INF)
        cout << a << endl;
      else
        cout << "-1" << endl;
    } else {
      if (n == 2) {
        if (a != INF && a != 0 && b != INF && c == INF)
          cout << a << b << endl;
        else if (a != INF && a != 0 && b == INF && vis[0] == 0)
          cout << a << 0 << endl;
        else
          cout << "-1" << endl;
      } else {
        if (a != INF && a != 0) {
          if (b == INF && c != 0 && c != INF && vis[0] == 0)
            cout << a << 0 << c << endl;
          else if (c == INF && b != INF && b != 0 && vis[0] == 0)
            cout << a << b << 0 << endl;
          else
            cout << -1 << endl;
        } else
          cout << "-1" << endl;
      }
    }
  }
  return 0;
}
