import Control.Monad
import Data.List

main = do
  li <- getLine
  let [n,m] = map read $ words li
  scs <- forM [1..m] (\_ -> do
    li <- getLine
    let [s,c] = map read $ words li
    return (s,c)
    )
  let ans = compute n scs
  print ans

compute :: Int -> [(Int,Int)] -> Int
compute n scs
  | not (ok c1) = -1
  | d1 == 0 = -1
  | otherwise = inner n
  where
    [c1,c2,c3] = [ nub $ findAll i scs | i <- [1..3] ]
    ok c = null c || singleton c
    d1 = head (c1 ++ [1])
    d2 = head (c2 ++ [0])
    d3 = head (c3 ++ [0])
    inner 1 | null c2 && null c3 = d1
    inner 2 | ok c2 && null c3 = d1 * 10 + d2
    inner 3 | ok c2 && ok c3 = d1 * 100 + d2 * 10 + d3
    inner _ = -1

findAll _ [] = []
findAll x ((y,z):yzs)
  | x == y = z : findAll x yzs
  | True   = findAll x yzs

singleton [_] = True
singleton _ = False
