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)

use std::io;
use std::str::FromStr;

fn input<T:FromStr>() -> T {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    line.trim().parse().ok().unwrap()
}

fn digit(n: u64) -> u32 {
    if n == 0 {
        return 1;
    }
    for i in 0..20u32 {
        if n < 10u64.pow(i) {
            return i as u32;
        }
    }
    20u32
}

fn main() {
    let n: u64 = input();
    if digit(n) == 1 {
        println!("{}", n);
        return;
    }
    let n_mid = (n - n / 10u64.pow(digit(n) - 1) * 10u64.pow(digit(n) - 1) - n % 10) / 10 ;
    // println!("{}", n_mid);

    let mut sum = 0;
    for i in 1..(n+1) {
        // println!("i = {}", i);
        let mut count = 0;
        let head = i / 10u64.pow(digit(i) - 1);
        let tail = i % 10;

        if tail == 0 {            
            continue;
        }

        // 一桁
        if head == tail {
            count += 1;
        }

        // 二桁以上
        for j in 1..7u32 {
            let max = (tail + 1) * 10u64.pow(j) - 10 + head;
            let min = tail * 10u64.pow(j) + head;            
            // println!("{} - {}", min, max);
            if max <= n {
                count += 10u64.pow(j - 1);
            } else if min > n {
                break;
            } else {
                // 最後の一桁で場合分け
                if head > n % 10 {
                    count += n_mid;
                } else {
                    count += n_mid + 1;
                }
            }
        }
        // println!("{}", count);
        sum += count;
    }
    println!("{}", sum);
}