Problem:
Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

1 \leq N \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008

Solution in Rust (1.15.1) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;
use std::collections::BTreeMap;

type Memo = BTreeMap<(usize, usize, usize, usize), i64>;

fn digits(mut n: usize) -> usize {
    let mut m = 0;
    while n > 0 {
        m += 1;
        n /= 10;
    }
    m
}

fn rec(s: usize, e: usize, m: usize, n: usize, memo: &mut Memo) -> i64 {
    let k = (s, e, m, n);
    if let Some(&res) = memo.get(&k) {
        return res;
    }
    let res = if m == 1 {
        if e <= n {
            1
        } else {
            0
        }
    } else {
        let d = 10usize.pow((m - 1) as u32);
        let mut res = 0;
        res += (n / d) as i64 * rec(s, e, m - 1, d - 1, memo);
        res += rec(s, e, m - 1, n % d, memo);
        res
    };
    memo.insert(k, res);
    res
}

fn solve_sub(n: usize, m: usize, v: &mut Vec<Vec<i64>>, memo: &mut Memo) {
    if m == 1 {
        for i in 1..=n {
            v[i][i] += 1;
        }
    } else {
        let d = 10usize.pow((m - 1) as u32);
        for s in 1..(n / d) {
            for e in 1..=9 {
                v[s][e] += rec(s, e, m - 1, d - 1, memo);
            }
        }
        for e in 1..=9 {
            v[n / d][e] += rec(n / d, e, m - 1, n % d, memo);
        }
        solve_sub(d - 1, m - 1, v, memo);
    }
}

fn solve(n: usize) -> i64 {
    let mut v = vec![vec![0; 10]; 10];
    let mut memo = Memo::new();
    let m = digits(n);
    solve_sub(n, m, &mut v, &mut memo);
    let mut ans = 0;
    for i in 1..=9 {
        for j in 1..=9 {
            ans += v[i][j] * v[j][i]
        }
    }
    ans
}

fn main() {
    input! {
        n: usize,
    }

    let ans = solve(n);
    println!("{}", ans);
}
*/

fn main() {
    let exe = "/tmp/bin8B080FDD";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";