Scanf.scanf "%d" (fun n ->
    let rec first k = if k < 10 then k else first (k / 10) in
    let count ap bp =
        let rec loop i acc =
            if i > n then acc else
                let s = first i in 
                let acc = if s = ap then acc + 1 else acc in
                loop (i + 10) acc
        in
        loop bp 0
    in
    let rec loop1 ap acc =
        let rec loop2 bp acc =
            if bp = 10 then acc else loop2 (bp + 1) (acc + count ap bp * count bp ap)
        in
        if ap = 10 then acc else
            loop1 (ap + 1) (loop2 1 acc)
    in
    loop1 1 0 |> Printf.printf "%d\n"
)