#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, ans = -1;
  cin >> N >> M;
  vector<int> s(M), c(M), s1(0), s2(0), s3(0);
  for (int i = 0; i < M; ++i) {
    cin >> s.at(i) >> c.at(i);
    if (s.at(i) == 1) s1.push_back(i);
    if (s.at(i) == 2) s2.push_back(i);
    if (s.at(i) == 3) s3.push_back(i);
  }
  if (s1.size() >= 2) {
    for (int i = 1; i < s1.size(); ++i) {
      if (c.at(s1.at(i - 1)) != c.at(s1.at(i))) {
        cout << ans << endl;
        return 0;
      }
    }
  } else if (s2.size() >= 2) {
    for (int i = 1; i < s2.size(); ++i) {
      if (c.at(s2.at(i - 1)) != c.at(s2.at(i))) {
        cout << ans << endl;
        return 0;
      }
    }
  } else if (s3.size() >= 2) {
    for (int i = 1; i < s3.size(); ++i) {
      if (c.at(s3.at(i - 1)) != c.at(s3.at(i))) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  if (N == 1) {
    if (s2.size() != 0 || s3.size() != 0) {
      cout << ans << endl;
      return 0;
    } else {
      for (int x = 0; x < 10; ++x) {
        for (int i = 0; i < M; ++i) {
          if (s.at(i) == 1 && c.at(i) == x) ans = x;
        }
      }
    }
  } else if (N == 2) {
    if (s3.size() != 0) {
      cout << ans << endl;
      return 0;
    } else {
      for (int x = 10; x < 100; ++x) {
        if (s1.size() != 0 && s2.size() != 0 && c.at(s1.at(0)) == x / 10 &&
            c.at(s2.at(0)) == x % 10) {
          cout << x << endl;
          return 0;
        } else if (s1.size() != 0 && s2.size() == 0 &&
                   c.at(s1.at(0)) == x / 10) {
          cout << x << endl;
          return 0;
        } else if (s1.size() == 0 && s2.size() != 0 &&
                   c.at(s2.at(0)) == x % 10) {
          cout << x << endl;
          return 0;
        }
      }
    }
  } else if (N == 3) {
    for (int x = 100; x < 1000; ++x) {
      if (s1.size() != 0 && s2.size() != 0 && s3.size() != 0 &&
          c.at(s1.at(0)) == x / 100 && c.at(s2.at(0)) == (x % 100) / 10 &&
          c.at(s3.at(0)) == (x % 100) % 10) {
        cout << x << endl;
        return 0;
      } else if (s1.size() != 0 && s2.size() != 0 && s3.size() == 0 &&
                 c.at(s1.at(0)) == x / 100 &&
                 c.at(s2.at(0)) == (x % 100) / 10) {
        cout << x << endl;
        return 0;
      } else if (s1.size() != 0 && s2.size() == 0 && s3.size() != 0 &&
                 c.at(s1.at(0)) == x / 100 &&
                 c.at(s3.at(0)) == (x % 100) % 10) {
        cout << x << endl;
        return 0;
      } else if (s1.size() == 0 && s2.size() != 0 && s3.size() != 0 &&
                 c.at(s2.at(0)) == (x % 100) / 10 &&
                 c.at(s3.at(0)) == (x % 100) % 10) {
        cout << x << endl;
        return 0;
      } else if (s1.size() != 0 && s2.size() == 0 && s3.size() == 0 &&
                 c.at(s1.at(0)) == x / 100) {
        cout << x << endl;
        return 0;
      } else if (s1.size() == 0 && s2.size() != 0 && s3.size() == 0 &&
                 c.at(s2.at(0)) == (x % 100) / 10) {
        cout << x << endl;
        return 0;
      } else if (s1.size() == 0 && s2.size() == 0 && s3.size() != 0 &&
                 c.at(s3.at(0)) == (x % 100) % 10) {
        cout << x << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
