#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, s[100], c[100], a[100];
  bool flag = false;
  cin >> N >> M;
  a[0] = 0;
  a[1] = 0;
  a[2] = 0;
  for (int i = 0; i < M; i++) {
    cin >> s[i] >> c[i];
  }
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < M; j++) {
      if (s[i] == s[j]) {
        if (c[i] == c[j] && i != j) {
          cout << s[i] << s[j] << c[i] << c[j] << endl;
          flag = true;
          if (s[i] == 1) a[0] = c[i];
          if (s[i] == 2) a[1] = c[i];
          if (s[i] == 3) a[2] = c[i];
        }
      }
    }
  }
  if (flag) {
    cout << a[0] << a[1] << a[2] << endl;
  } else {
    cout << "-1" << endl;
  }
}
