#include <bits/stdc++.h>
using namespace std;
inline long long toint(string s) {
  long long v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const long long INF = 100000000000000;
const long long NINF = -100000000000000;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long nCr(long long n, long long r) {
  if (n == 0) {
    return 0;
  }
  if (r == 0) {
    return 1;
  }
  if (r == 1) {
    return n;
  }
  if (n == r) {
    return 1;
  }
  if (r > n / 2) {
    r = n / 2;
  }
  long long result = 1;
  for (long long i = 1; i <= r; i++) {
    result *= (n - i + 1) / i;
  }
  return (result);
}
pair<long long, long long> maxP(vector<long long> a, long long size) {
  pair<long long, long long> p;
  vector<long long>::iterator iter = max_element(a.begin(), a.end());
  p.first = *iter;
  p.second = distance(a.begin(), iter);
  return p;
}
pair<long long, long long> minP(vector<long long> a, long long size) {
  pair<long long, long long> p;
  vector<long long>::iterator iter = min_element(a.begin(), a.end());
  p.first = *iter;
  p.second = distance(a.begin(), iter);
  return p;
}
long long sumL(vector<long long> a, long long size) {
  long long sum = 0;
  for (int i = (0); i < (size); ++i) {
    sum += a[i];
  }
  return sum;
}
long long counT(vector<long long> a, long long t) {
  sort(a.begin(), a.end());
  return upper_bound(a.begin(), a.end(), t) -
         lower_bound(a.begin(), a.end(), t);
}
long long DIV[1000 + 1][1000 + 1];
void divide(long long n, long long m) {
  DIV[0][0] = 1;
  for (int i = (1); i < (n + 1); ++i) {
    DIV[i][0] = 0;
  }
  for (int i = (0); i < (n + 1); ++i) {
    DIV[i][1] = 1;
  }
  for (int i = (1); i < (m + 1); ++i) {
    for (int t = (0); t < (n + 1); ++t) {
      if (DIV[t][i] > 0) continue;
      if (t >= i) {
        DIV[t][i] = DIV[t - i][i] + DIV[t][i - 1];
      } else {
        DIV[t][i] = DIV[t][i - 1];
      }
    }
  }
}
long long search(vector<long long> &a, long long n) {
  std::vector<long long>::iterator iter = std::find(a.begin(), a.end(), n);
  size_t index = distance(a.begin(), iter);
  return index;
}
class Judge {
 public:
  vector<bool> IS_PRIME_CONTAINER;
  Judge(int max_) : IS_PRIME_CONTAINER(max_, true) { max = max_; }
  int max;
  void IsPrime_init() {
    IS_PRIME_CONTAINER[0] = false;
    IS_PRIME_CONTAINER[1] = false;
    for (int i = (2); i < (sqrt(max) + 1); ++i) {
      if (IS_PRIME_CONTAINER[i]) {
        for (int j = (2); j < ((max) / i + 1); ++j) {
          if (j * i <= max) {
            IS_PRIME_CONTAINER[i * j] = false;
          }
        }
      }
    }
  }
  inline bool IsPrime(long long num) { return IS_PRIME_CONTAINER[num]; }
};
class Bellman {
 public:
  vector<long long> DIST;
  Bellman(int max_) : DIST(max_, INF) { max = max_; }
  ~Bellman() { std::vector<long long>().swap(DIST); }
  int max;
  struct E {
    long long from, to, cost;
  };
  void init() {
    for (int i = (0); i < (max); ++i) {
      DIST[i] = INF;
    }
  }
  void shortest(long long s, vector<E> &x) {
    for (int i = (0); i < (100000); ++i) {
      DIST[i] = INF;
    }
    DIST[s] = 0;
    while (1) {
      bool t = false;
      for (int i = (0); i < (x.size()); ++i) {
        E h = x[i];
        if (DIST[h.from] != INF && DIST[h.to] > DIST[h.from] + h.cost) {
          DIST[h.to] = DIST[h.from] + h.cost;
          t = true;
        }
      }
      if (!t) {
        break;
      }
    }
  }
};
class dijkstra {
 public:
  int V;
  struct edge {
    int to;
    int cost;
  };

 public:
  vector<long long> dist;
  vector<vector<edge> > G;
  dijkstra(long long V_) : G(V_ * V_) {
    for (int i = (0); i < (V_); ++i) {
      dist.push_back(INF);
    }
    V = V_;
  }
  ~dijkstra() {
    std::vector<long long>().swap(dist);
    vector<std::vector<edge> >().swap(G);
  }
  void init() {
    for (int i = (0); i < (dist.size()); ++i) dist[i] = INF;
  }
  void shortest(int s) {
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long> >,
                   greater<pair<long long, long long> > >
        que;
    dist[s] = 0;
    que.push(pair<long long, long long>(0, s));
    while (!que.empty()) {
      pair<long long, long long> p = que.top();
      que.pop();
      int v = p.second;
      if (dist[v] < p.first) continue;
      for (int i = 0; i < G[v].size(); ++i) {
        edge e = G[v][i];
        if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = dist[v] + e.cost;
          que.push(pair<long long, long long>(dist[e.to], e.to));
        }
      }
    }
  }
};
class UnionFind {
 public:
  vector<long long> par;
  vector<long long> rank;
  UnionFind(long long N) : par(N), rank(N) {
    for (int i = (0); i < (N); ++i) par[i] = i;
    for (int i = (0); i < (N); ++i) rank[i] = 0;
  }
  ~UnionFind() {}
  long long root(long long x) {
    if (par[x] == x)
      return x;
    else {
      par[x] = root(par[x]);
      return par[x];
    }
  }
  void unite(long long x, long long y) {
    long long rx = root(x);
    long long ry = root(y);
    if (rx == ry) return;
    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry]) {
        rank[rx]++;
      }
    }
  }
  bool same(long long x, long long y) {
    long long rx = root(x);
    long long ry = root(y);
    return rx == ry;
  }
};
class BFS {
 public:
  BFS(vector<vector<char> > field_, long long h_, long long w_) {
    field = field_;
    h = h_;
    w = w_;
    initial_number = INF;
    for (int i = (0); i < (h); ++i) {
      dist.push_back(vector<long long>(w, initial_number));
    }
  }
  ~BFS() {
    std::vector<std::vector<long long> >().swap(dist);
    std::vector<std::vector<char> >().swap(field);
  }
  vector<vector<char> > field;
  long long h;
  long long w;
  long long initial_number;
  vector<vector<long long> > dist;
  pair<long long, long long> plus(pair<long long, long long> &a,
                                  pair<long long, long long> &b) {
    pair<long long, long long> p;
    p.first = a.first + b.first;
    p.second = a.second + b.second;
    return p;
  }
  bool equal(pair<long long, long long> &a, pair<long long, long long> &b) {
    return (a.first == b.first && a.second == b.second);
  }
  bool is_in_field(int h, int w, const pair<long long, long long> &point) {
    const int c = point.second;
    const int r = point.first;
    return (0 <= c && c < w) && (0 <= r && r < h);
  }
  void init() {
    for (int i = (0); i < (dist.size()); ++i) {
      for (int t = (0); t < (dist[i].size()); ++t) {
        dist[i][t] = initial_number;
      }
    }
  }
  void shortest(long long sy, long long sx) {
    init();
    pair<long long, long long> c[4];
    c[0].first = 0;
    c[0].second = 1;
    c[1].first = 0;
    c[1].second = -1;
    c[2].first = 1;
    c[2].second = 0;
    c[3].first = -1;
    c[3].second = 0;
    queue<pair<long long, long long> > Q;
    pair<long long, long long> s;
    s.first = sy;
    s.second = sx;
    dist[sy][sx] = 0;
    Q.push(s);
    while (Q.empty() == false) {
      pair<long long, long long> now = Q.front();
      Q.pop();
      for (int u = 0; u < 4; u++) {
        pair<long long, long long> x = c[u];
        pair<long long, long long> next = plus(now, x);
        if (is_in_field(h, w, next)) {
          if (field[next.first][next.second] == '.') {
            if (dist[next.first][next.second] == initial_number) {
              dist[next.first][next.second] = dist[now.first][now.second] + 1;
              Q.push(next);
            } else {
            }
          }
        }
      }
    }
  }
};
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
class unorder {
 public:
  T a, b;
  unorder(T a_, T b_) {
    (a = a_);
    (b = b_);
  }
  ~unorder() {}

 public:
  bool operator==(unorder y) {
    return (this->a + this->b == y.a + y.b && this->a * this->b == y.a * y.b);
  }
  bool operator!=(unorder y) {
    return (this->a + this->b != y.a + y.b || this->a * this->b != y.a * y.b);
  }
  void operator=(pair<T, T> p) {
    this->a = p.first;
    this->b = p.second;
  }
};
vector<pair<long long, long long> > PrimeFactors(long long n) {
  std::map<long long, long long> out;
  vector<pair<long long, long long> > answer;
  while (n % 2 == 0) {
    ++out[2];
    n = n / 2;
  }
  if (out[2]) answer.push_back(make_pair(2, out[2]));
  for (long long i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      ++out[i];
      n = n / i;
    }
    if (out[i]) answer.push_back(make_pair(i, out[i]));
  }
  if (n > 2) ++out[n];
  if (out[n]) answer.push_back(make_pair(n, out[n]));
  return answer;
}
class ModComb {
 public:
  vector<long long> fac;
  vector<long long> finv;
  vector<long long> inv;
  ModComb(long long MAX_, long long MOD_) : fac(MAX_), finv(MAX_), inv(MAX_) {
    MAX = MAX_;
    MOD = MOD_;
  }
  ~ModComb() {
    std::vector<long long>().swap(fac);
    std::vector<long long>().swap(finv);
    std::vector<long long>().swap(inv);
  }
  int MAX;
  int MOD;
  void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
      fac[i] = fac[i - 1] * i % MOD;
      inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
      finv[i] = finv[i - 1] * inv[i] % MOD;
    }
  }
  long long COM(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
  }
};
void BIT(vector<vector<long long> > &a, long long n) {
  for (long long bit = 0; bit < (1 << n); bit++) {
    vector<long long> P;
    a.push_back(P);
    for (long long i = 0; i < n; i++) {
      if (bit & (1 << i)) {
        a[bit].push_back(i);
      }
    }
  }
}
int getdigit(long long n) { return log10(n) + 1; }
long long keta(long long n, long long k) {
  if (k > getdigit(n) * (n != 0) + (n == 0)) {
    return -1;
  } else if (k == 1) {
    return n % 10;
  } else {
    long long p = 1;
    for (int i = (0); i < (k); ++i) p *= 10;
    return (n % p) / (p / 10);
  }
}
long long kiriage(long long a, long long b) { return a / b + (a % b == 0); }
template <class T>
pair<vector<long long>, T> seqence(T x) {
  pair<vector<long long>, T> s;
  bool flag = 0;
  long long now;
  long long seq = 1;
  long long next;
  for (int i = (0); i < (x.size()); ++i) {
    if (i + 1 >= x.size()) {
      now = x[i];
      s.first.push_back(seq);
      s.second.push_back(now);
      break;
    } else {
      now = x[i];
      next = x[i + 1];
      if (flag) {
        if (now == next) {
          seq++;
        } else {
          s.first.push_back(seq);
          s.second.push_back(now);
          seq = 1;
          flag = 0;
        }
      } else {
        if (now == next) {
          seq++;
          flag = 1;
        } else {
          s.first.push_back(seq);
          s.second.push_back(now);
          seq = 1;
        }
      }
    }
  }
  return s;
}
void StringSort(vector<string> &S) {
  map<int, int> SwapInd;
  int maxi = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S[i].size() > maxi) maxi = S[i].size();
  }
  for (int i = 0; i < maxi; i++) {
    int k = maxi - i;
    int sort_num = 0;
    for (int j = 0; j < S.size(); j++) {
      if (S[j].size() >= k) {
        SwapInd[j] = 1;
        sort_num++;
      }
    }
    int swapped = 0;
    for (int j = 0; j < S.size(); j++) {
      if (SwapInd[j] == 1 && j < S.size() - sort_num) {
        if (SwapInd[S.size() - sort_num + swapped] == 0) {
          iter_swap(S.begin() + j, S.begin() + S.size() - sort_num + swapped);
        } else {
          while (SwapInd[S.size() - sort_num + swapped] != 0) {
            swapped++;
            if (S.size() - sort_num + swapped >= S.size()) {
              break;
            }
            if (S.size() - sort_num + swapped < S.size()) {
              iter_swap(S.begin() + j,
                        S.begin() + S.size() - sort_num + swapped);
            }
          }
        }
        SwapInd[j] = 0;
        SwapInd[S.size() - sort_num + swapped] = 0;
        swapped++;
      }
    }
    vector<string> WhatSorted(sort_num);
    vector<pair<int, int> > char_and_index(sort_num);
    for (int j = 0; j < sort_num; j++) {
      if (S[S.size() - sort_num + j][k - 1] >= 65 &&
          S[S.size() - sort_num + j][k - 1] <= 90) {
        char_and_index[j].first = (int)S[S.size() - sort_num + j][k - 1] + 32;
      } else {
        char_and_index[j].first = (int)S[S.size() - sort_num + j][k - 1];
      }
      char_and_index[j].second = j;
      WhatSorted[j] = S[S.size() - sort_num + j];
    }
    sort(char_and_index.begin(), char_and_index.end());
    for (int j = 0; j < sort_num; j++) {
      S[S.size() - sort_num + j] = WhatSorted[char_and_index[j].second];
    }
  }
}
vector<vector<long long> > pascal(66);
vector<long long> getpascal(long long n) {
  if (pascal[n].size() > 0) return pascal[n];
  vector<long long> e;
  if (n == 0) {
    e.push_back(1);
  } else {
    e.push_back(1);
    vector<long long> ajj = getpascal(n - 1);
    for (int i = (0); i < (ajj.size() - 1); ++i) {
      e.push_back(ajj[i] + ajj[i + 1]);
    }
    e.push_back(1);
  }
  return pascal[n] = e;
}
void pascal_calc(long long n) { pascal[n] = getpascal(n); }
vector<vector<long long> > Sigma(11, vector<long long>(1000005));
long long sigmaCalc(long long a, long long n) {
  if (Sigma[a][n] != 0) {
    return Sigma[a][n];
  }
  pascal_calc(a + 1);
  long long ans = 0;
  if (a == 0) {
    ans = n;
  } else {
    long long coefficient = pascal[a + 1][1];
    ans += pow(n + 1, a + 1) - 1;
    for (int i = (0); i < (a); ++i) {
      ans -= sigmaCalc(a - 1 - i, n) * pascal[a + 1][2 + i];
    }
    ans /= coefficient;
  }
  return Sigma[a][n] = ans;
}
string s, t;
int dp[4000][4000];
int solve(int i, int j) {
  if (i < 0 || j < 0) {
    return 0;
  }
  if (dp[i][j] != 0) {
    return dp[i][j];
  }
  int ans;
  if (s[i] == t[j]) {
    ans = solve(i - 1, j - 1) + 1;
  } else {
    ans = max(solve(i - 1, j), solve(i, j - 1));
  }
  return dp[i][j] = ans;
}
int main() {
  long long n, m;
  cin >> n >> m;
  long long u = 0;
  vector<long long> s(m), c(m);
  map<long long, long long> o;
  for (int i = (0); i < (m); ++i) {
    cin >> s[i] >> c[i];
    if (o[s[i]] != c[i] && o[s[i]] != 0) {
      u = 1;
    }
    o[s[i]] = c[i];
  }
  if (u) {
    cout << -1;
    return 0;
  }
  for (int i = (0); i < (10); ++i) {
    for (int j = (0); j < (10); ++j) {
      for (int k = (0); k < (10); ++k) {
        long long an = 1;
        for (int g = (0); g < (m); ++g) {
          map<long long, long long> pp;
          pp[1] = i;
          pp[2] = j;
          pp[3] = k;
          if (c[g] != pp[s[g]]) {
            an = 0;
          }
        }
        if (an) {
          cout << i * 100 + j * 10 + k;
          return 0;
        }
      }
    }
  }
  cout << -1;
  return 0;
}
