#include <bits/stdc++.h>
int main(void) {
  int n, m, s[100], c[100], ans = 0, count = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &s[i], &c[i]);
  }
  for (int i = 1; i <= 3; i++) {
    if (n == 3 && s[i] == 3 && c[s[i]] == 0) {
      printf("-1");
      return 0;
    } else if (n == 2 && s[i] == 2 && c[s[i]] == 0) {
      printf("-1");
      return 0;
    }
  }
  for (int j = 1; j <= m - 1; j++) {
    for (int k = j + 1; k <= m; k++) {
      if (s[j] == s[k] && c[j] != c[k]) {
        printf("-1");
        return 0;
      } else if (s[j] == s[k] && c[j] == c[k]) {
        c[j] = -1;
      }
    }
  }
  if (n == 3) {
    for (int k = 1; k <= m; k++) {
      if (s[k] == 1 && c[k] > 0) {
        ans += c[k] * 100;
        count++;
      } else if (s[k] == 2 && c[k] > 0) {
        ans += c[k] * 10;
        count++;
      } else if (s[k] == 3 && c[k] > 0) {
        ans += c[k];
        count++;
      }
    }
  } else if (n == 2) {
    for (int k = 1; k <= m; k++) {
      if (s[k] == 1 && c[k] > 0) {
        ans += c[k] * 10;
      } else if (s[k] == 2 && c[k] > 0) {
        ans += c[k];
      }
    }
  } else if (n == 1) {
    for (int k = 1; k <= m; k++) {
      if (s[k] == 1 && c[k] > 0) {
        ans += c[k];
      }
    }
  }
  if (count < n) {
    printf("-1");
    return 0;
  }
  printf("%d", ans);
}
