#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s;
  vector<int> c;
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    int S, C;
    cin >> S >> C;
    s.push_back(S);
    c.push_back(C);
  }
  int a = 10;
  bool a_f = false;
  int b = 10;
  bool b_f = false;
  int d = 10;
  bool d_f = false;
  bool flag = false;
  if (n == 1) {
    for (int i = 0; i < m; i++) {
      if (s[i] == 1 && d_f != true) {
        d = min(d, c[i]);
        d_f = true;
      } else if (s[i] == 1 && d_f && d != c[i]) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (d == 10) {
      cout << -1 << endl;
      return 0;
    }
    cout << d << endl;
    return 0;
  } else if (n == 2) {
    for (int i = 0; i < m; i++) {
      if (s[i] == 1 && b_f != true) {
        b = min(b, c[i]);
        b_f = true;
      } else if (s[i] == 1 && b_f && b != c[i]) {
        cout << -1 << endl;
        return 0;
      } else if (s[i] == 2 && d_f != true) {
        d = min(d, c[i]);
        d_f = true;
        if (d == 0) flag = true;
      } else if (s[i] == 2 && d_f && d != c[i]) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (b == 10) b = 0;
    if (d == 10) d = 0;
    if (b == 0 && d == 0) {
      if (flag) {
        cout << d << endl;
        return 0;
      }
      cout << -1 << endl;
      return 0;
    } else if (b == 0 && d != 0) {
      cout << d << endl;
    } else {
      cout << b << d << endl;
    }
    return 0;
  } else if (n == 3) {
    for (int i = 0; i < m; i++) {
      if (s[i] == 1 && a_f != true) {
        a = min(a, c[i]);
        a_f = true;
      } else if (s[i] == 1 && a_f && a != c[i]) {
        cout << -1 << endl;
        return 0;
      } else if (s[i] == 2 && b_f != true) {
        b = min(b, c[i]);
        b_f = true;
      } else if (s[i] == 2 && b_f && b != c[i]) {
        cout << -1 << endl;
        return 0;
      } else if (s[i] == 3 && d_f != true) {
        d = min(d, c[i]);
        d_f = true;
        if (d == 0) flag = true;
      } else if (s[i] == 3 && d_f && d != c[i]) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (a == 10) a = 0;
    if (b == 10) b = 0;
    if (d == 10) d = 0;
    if (a == 0 && b == 0 && d == 0) {
      if (flag) {
        cout << d << endl;
        return 0;
      }
      cout << -1 << endl;
      return 0;
    } else if (a == 0 && b != 0 && d != 0) {
      cout << b << d << endl;
    } else if (a == 0 && b != 0 && d == 0) {
      cout << b << d << endl;
    } else if (a == 0 && b == 0 && d != 0) {
      cout << d << endl;
    } else {
      cout << a << b << d << endl;
    }
  }
  return 0;
}
