#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int cnt = -1;
  int checknumber = 1001;
  int c1 = 1001, c2 = 1001, c3 = 1001;
  vector<vector<int>> a(m, vector<int>(2));
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> a[i][j];
    }
  }
  if (n == 1) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < 2; j++) {
        if (a[i][0] > 1) {
          cout << cnt << endl;
          return 0;
        } else {
          if (a[i][1] == 0) {
            cout << cnt << endl;
            return 0;
          } else {
            checknumber = min(checknumber, a[i][1]);
          }
        }
      }
    }
  } else if (n == 2) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < 2; j++) {
        if (a[i][0] > 2) {
          cout << cnt << endl;
          return 0;
        } else {
          if (a[i][0] == 1) {
            c1 = min(c1, a[i][1]);
          } else {
            c1 = 0;
          }
          if (a[i][0] == 2) {
            c2 = min(c2, a[i][1]);
          } else {
            c2 = 0;
          }
        }
      }
    }
    checknumber = c2 * 10 + c1;
  } else if (n == 3) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < 2; j++) {
        if (a[i][0] == 1) {
          c1 = min(c1, a[i][1]);
        } else {
          c1 = 0;
        }
        if (a[i][0] == 2) {
          c2 = min(c2, a[i][1]);
        } else {
          c2 = 0;
        }
        if (a[i][0] == 3) {
          c3 = min(c3, a[i][1]);
        } else {
          c3 = 0;
        }
      }
    }
    checknumber = c3 * 100 + c2 * 10 + c1;
  }
  cout << checknumber << endl;
  return 0;
}
