#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, s, c, c1 = 10, c2 = 10, c3 = 10;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> s >> c;
    if (s == 1) {
      if (c == 0) {
        c1 = -1;
      } else if (c1 == 10) {
        c1 = c;
      } else {
        if (c1 == c) {
        } else {
          c1 = -1;
        }
      }
    } else if (s == 2) {
      if (c2 == 10) {
        c2 = c;
      } else {
        if (c2 == c) {
        } else {
          c2 = -1;
        }
      }
    } else {
      if (c3 == 10) {
        c3 = c;
      } else {
        if (c3 == c) {
        } else {
          c3 = -1;
        }
      }
    }
  }
  if ((c1 == -1) || (c2 == -1) || (c3 == -1)) {
    cout << "-1" << endl;
  } else if (!(c1 == 10)) {
    cout << c1;
    if (!(c2 == 10) && (N >= 2)) {
      cout << c2;
      if (!(c3 == 10) && (N >= 3)) {
        cout << c3;
      } else if ((c3 == 10) && (N >= 3)) {
        cout << 0;
      } else {
      }
    } else if ((c2 == 10) && (N >= 2)) {
      cout << 0;
      if (!(c3 == 10) && (N >= 3)) {
        cout << c3;
      } else if ((c3 == 10) && (N >= 3)) {
        cout << 0;
      } else {
      }
    } else {
    }
  } else {
    if (!(c2 == 10) && (N >= 2)) {
      cout << c2;
      if (!(c3 == 10) && (N >= 3)) {
        cout << c3;
      } else if ((c3 == 10) && (N >= 3)) {
        cout << 0;
      } else {
      }
    } else if ((c2 == 10) && (N >= 2)) {
      cout << 0;
      if (!(c3 == 10) && (N >= 3)) {
        cout << c3;
      } else if ((c3 == 10) && (N >= 3)) {
        cout << 0;
      } else {
      }
    } else {
      cout << 0;
    }
  }
}
