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)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::*;

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

fn read<T: FromStr>() -> T {
  let stdin = stdin();
  let stdin = stdin.lock();
  let token: String = stdin
    .bytes()
    .map(|c| c.expect("failed reading char") as char)
    .skip_while(|c| c.is_whitespace())
    .take_while(|c| !c.is_whitespace())
    .collect();
  token.parse().ok().expect("failed parsing")
}

fn main() {
  let n: usize = read::<usize>();

  let mut ans = 0;
  for head in 1..10 {
    for end in 1..10 {
      ans += (count(n, head, end)) * count(n, end, head);
    }
  }
  println!("{}", ans);
}

fn to_int(n: &str) -> usize {
  if n.is_empty() {
    0
  } else {
    n.parse::<usize>().unwrap()
  }
}

fn decompose(x: usize) -> (usize, usize, usize) {
  let s = x.to_string();
  let len = s.len();
  let head = to_int(&s[0..1]);
  let end = to_int(&s[(len - 1)..len]);
  let mid = to_int(&s[1..std::cmp::max(len - 1, 1)]);
  (head, mid, end)
}

fn count(x: usize, head: usize, end: usize) -> usize {
  let x_str: String = x.to_string();
  let digit: usize = x_str.len();

  let (xh, xm, xe) = decompose(x);

  let mut res = 0;

  if head == end && head <= x {
    res += 1;
  }

  let ten: usize = 10;

  for d in 2..digit {
    res += ten.pow((d - 2) as u32);
  }

  if digit >= 2 {
    if head < xh {
      res += ten.pow((digit - 2) as u32);
    } else if head == xh {
      if end <= xe {
        res += xm + 1;
      } else {
        res += xm;
      }
    }
  }

  res
}