#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int s[3] = {0};
  cin >> n >> m;
  int bit;
  int data;
  if (n == 1) {
    for (int i = 0; i < m; i++) {
      cin >> bit >> data;
      if (s[bit - 1] == 0 && data != 0) {
        s[bit - 1] = data;
      }
      if (s[bit - 1] > data) {
        s[bit - 1] = data;
      }
    }
    cout << s[0];
    return 0;
  }
  if (n == 2) {
    for (int i = 0; i < m; i++) {
      cin >> bit >> data;
      if (s[bit - 1] == 0 && data != 0) {
        s[bit - 1] = data;
      }
      if (s[bit - 1] > data) {
        s[bit - 1] = data;
      }
    }
    if (s[0] == 0) {
      cout << -1;
      return 0;
    } else {
      cout << s[0] * 10 + s[1];
      return 0;
    }
  }
  if (n == 3) {
    for (int i = 0; i < m; i++) {
      cin >> bit >> data;
      if (s[bit - 1] == 0 && data != 0) {
        s[bit - 1] = data;
      }
      if (s[bit - 1] > data) {
        s[bit - 1] = data;
      }
    }
    if (s[0] == 0) {
      cout << -1;
      return 0;
    } else {
      cout << s[0] * 100 + s[1] * 10 + s[2];
      return 0;
    }
  }
  return 0;
}
