#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int N, M, ans = 0;
  cin >> N >> M;
  vector<int> s(M), c(M);
  for (int i = 0; i < (int)M; ++i) cin >> s[i] >> c[i];
  if (N == 1) {
    if (M == 0) {
      cout << -1 << endl;
      goto xyz;
    } else if (M == 1) {
      cout << c[0] << endl;
      goto xyz;
    } else {
      for (int j = 0; j < M - 1; ++j) {
        for (int k = j + 1; k < M; ++k) {
          if (c[j] != c[k]) {
            cout << -1 << endl;
            goto xyz;
          }
        }
      }
      cout << c[0] << endl;
      goto xyz;
    }
  } else if (N == 2) {
    if (M == 0) {
      cout << -1 << endl;
      goto xyz;
    } else if (M == 1) {
      if (s[0] == 1 && c[0] != 0) {
        cout << c[0] * 10 << endl;
        goto xyz;
      } else if (s[0] == 1 && c[0] == 0) {
        cout << -1 << endl;
        goto xyz;
      } else if (s[0] == 2) {
        cout << 10 + c[0] << endl;
        goto xyz;
      }
    } else {
      int t = 0;
      for (int i = 0; i < (int)M; ++i) t += s[i] - 2;
      if (t == 0) {
        for (int j = 0; j < M - 1; ++j) {
          for (int k = j + 1; k < M; ++k) {
            if (c[j] != c[k]) {
              cout << -1 << endl;
              goto xyz;
            }
          }
        }
        cout << 10 + c[0] << endl;
        goto xyz;
      } else {
        for (int j = 0; j < M - 1; ++j) {
          for (int k = j + 1; k < M; ++k) {
            if (s[j] == 1 && s[k] == 1 && c[j] != c[k]) {
              cout << -1 << endl;
              goto xyz;
            }
            if (s[j] == 2 && s[k] == 2 && c[j] != c[k]) {
              cout << -1 << endl;
              goto xyz;
            }
          }
        }
        for (int i = 0; i < (int)M; ++i)
          if (s[i] == 1) {
            ans += 10 * c[i];
            break;
          }
        for (int i = 0; i < (int)M; ++i)
          if (s[i] == 2) {
            ans += c[i];
            break;
          }
        if (ans < 10) {
          cout << -1 << endl;
          goto xyz;
        }
        cout << ans << endl;
        goto xyz;
      }
    }
  } else {
    if (M == 0) {
      cout << -1 << endl;
      goto xyz;
    } else if (M == 1) {
      if (s[0] == 1 && c[0] != 0) {
        cout << c[0] * 100 << endl;
        goto xyz;
      } else if (s[0] == 1 && c[0] == 0) {
        cout << -1 << endl;
        goto xyz;
      } else if (s[0] == 2) {
        cout << 100 + c[0] * 10 << endl;
        goto xyz;
      } else if (s[0] == 3) {
        cout << 100 + c[0] << endl;
        goto xyz;
      }
    } else {
      int t = 0;
      for (int i = 0; i < (int)M; ++i)
        if (s[i] == 2 || s[i] == 3) {
          ++t;
        }
      if (t == M) {
        for (int j = 0; j < M - 1; ++j) {
          for (int k = j + 1; k < M; ++k) {
            if (s[j] == 2 && s[k] == 2 && c[j] != c[k]) {
              cout << -1 << endl;
              goto xyz;
            }
            if (s[j] == 3 && s[k] == 3 && c[j] != c[k]) {
              cout << -1 << endl;
              goto xyz;
            }
          }
        }
        for (int i = 0; i < (int)M; ++i)
          if (s[i] == 2) {
            ans += 10 * c[i];
            break;
          }
        for (int i = 0; i < (int)M; ++i)
          if (s[i] == 3) {
            ans += c[i];
            break;
          }
        cout << ans + 100 << endl;
        goto xyz;
      } else {
        for (int j = 0; j < M - 1; ++j) {
          for (int k = j + 1; k < M; ++k) {
            if (s[j] == 1 && s[k] == 1 && c[j] != c[k]) {
              cout << -1 << endl;
              goto xyz;
            }
            if (s[j] == 2 && s[k] == 2 && c[j] != c[k]) {
              cout << -1 << endl;
              goto xyz;
            }
            if (s[j] == 3 && s[k] == 3 && c[j] != c[k]) {
              cout << -1 << endl;
              goto xyz;
            }
          }
        }
        for (int i = 0; i < (int)M; ++i)
          if (s[i] == 1) {
            ans += 100 * c[i];
            break;
          }
        for (int i = 0; i < (int)M; ++i)
          if (s[i] == 2) {
            ans += 10 * c[i];
            break;
          }
        for (int i = 0; i < (int)M; ++i)
          if (s[i] == 3) {
            ans += c[i];
            break;
          }
        if (ans < 100) {
          cout << -1 << endl;
          goto xyz;
        }
        cout << ans << endl;
        goto xyz;
      }
    }
  }
xyz:;
}
