use proconio::input;
 
fn main() {
  input! {
    N: usize,
    M: usize,
    A: [(usize, usize);M]
  }
  
  if M == 0 {
    println!("{}", 0);
    return
  }
  
  let mut result: [usize;3] = [10,10,10];
  
  for (a, b) in A {
    if result[a-1] == 10 || result[a-1] == b {
      result[a-1] = b;
    } else {
      println!("-1");
      return
    }
  }
  
  if result[0] == 0 {
    println!("-1");
    return
  } else if result[0] == 10 && result[1] == 0 {
    println!("-1");
    return
  }

  let one = result[2];  
  let ten = if result[1] == 10 {
    0
  } else {
    result[1] * 10
  };
  
  let hundred = if result[0] == 10 {
    0
  } else {
    result[0] * 100
  };
  
  println!("{}", hundred + ten + one);
}