#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
inline int two(int n) { return 1 << n; }
inline int last_bit(int n) { return n & (-n); }
inline int test(int n, int b) { return (n >> b) & 1; }
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline void set_bit(int &n, int b) { n |= two(b); }
inline int ones(int n) {
  int res = 0;
  while (n && ++res) n -= n & (-n);
  return res;
}
template <class T>
void chmax(T &a, const T &b) {
  a = max(a, b);
}
template <class T>
void chmin(T &a, const T &b) {
  a = min(a, b);
}
int main() {
  int n, m;
  cin >> n >> m;
  int s[6];
  int c[6];
  int ans1 = 10, ans2 = 10, ans3 = 10;
  for (long long int i = (0); i < (m); i++) {
    cin >> s[i];
    cin >> c[i];
    if (s[i] == 1 && c[i] != 0) chmin(ans1, c[i]);
    if (s[i] == 2) chmin(ans2, c[i]);
    if (s[i] == 3) chmin(ans3, c[i]);
  }
  if (n != 1) {
    if (ans1 == 10)
      cout << -1;
    else {
      cout << ans1;
      if (ans2 == 10) ans2 = 0;
      if (ans3 == 10) ans3 = 0;
      if (n >= 2) cout << ans2;
      if (n == 3) cout << ans3;
    }
  }
  if (n == 1) {
    ans1 = 10;
    for (long long int i = (0); i < (m); i++) chmin(ans1, c[i]);
    if (ans1 == 10) ans1 = 0;
    cout << ans1;
  }
  return 0;
}
