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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE BangPatterns #-}

-- Default Imports

import Control.Monad
import Control.Monad.ST
import Control.Monad.Trans.Class

import qualified Control.Monad.Reader as Reader
import Control.Monad.Reader (Reader, ReaderT, runReader, runReaderT)

import qualified Data.Array as Array
import qualified Data.Array.IArray as IArray
import qualified Data.Array.MArray as MArray
import qualified Data.Array.ST as STArray
import Data.Array.ST (STArray, STUArray, runSTArray, runSTUArray)
import Data.Ix (Ix)

import qualified Data.Bits as Bits
import Data.Bits ((.&.), (.|.))

import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Char8 (ByteString)

import qualified Data.Char as Char
import qualified Data.List as List
import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
import Data.Set (Set)

import qualified Data.Vector as Vector
import qualified Data.Vector.Generic as GVector
import qualified Data.Vector.Mutable as MVector
import Data.Vector.Mutable (STVector)
import qualified Data.Vector.Unboxed as UVector

import Numeric

import Debug.Trace
-- ----------

-- Custom Import Here


-- ----------

-- Default Definitions

type Vector = Vector.Vector
-- type GVector = GVector.Vector
type UVector = UVector.Vector

readIntToList :: ByteString -> [Int]
readIntToList = List.unfoldr (fmap (second $ BS.dropWhile Char.isSpace) . BS.readInt)

readToList :: Read a => ByteString -> [a]
readToList = List.unfoldr $ fmap (mapTaken . mapRemain) . span' (not . Char.isSpace)
	where
		mapTaken  = first $ read . BS.unpack
		mapRemain = second $ BS.dropWhile Char.isSpace
		span' pred s
			| BS.null taken = Nothing
			| otherwise     = Just (taken, remain')
			where
				(taken, remain) = BS.span pred s
				remain' = BS.dropWhile Char.isSpace remain

readToListN :: Read a => Int -> ByteString -> [a]
readToListN n = take n . readToList

readIntToVector :: ByteString -> Vector Int
readIntToVector = Vector.unfoldr (fmap stripRemain . BS.readInt)
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToUVector :: ByteString -> UVector.Vector Int
readIntToUVector = UVector.unfoldr (fmap stripRemain . BS.readInt)
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToVectorN :: Int -> ByteString -> Vector.Vector Int
readIntToVectorN n = Vector.unfoldrN n (fmap stripRemain . BS.readInt)
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToUVectorN :: Int -> ByteString -> UVector.Vector Int
readIntToUVectorN n = UVector.unfoldrN n (fmap stripRemain . BS.readInt)
	where stripRemain = second $ BS.dropWhile Char.isSpace


first :: (a -> a') -> (a, b) -> (a', b)
first f (a, b) = (f a, b)

second :: (b -> b') -> (a, b) -> (a, b')
second f (a, b) = (a, f b)

average :: Fractional a => [a] -> a
average xs = sum xs / (fromIntegral . length) xs

-- ----------

ndigit :: Int -> Int
ndigit = ndigit' 0
	where
		ndigit' n 0 = n
		ndigit' n i = ndigit' (n + 1) (i `div` 10)

countIJ' :: Int -> Int -> Int -> Int
countIJ' n i j
	| n == 0 = 0
	| n == 1 = if i == j then 1 else 0
	| n == 2 = if i == j then 2 else 1
	| otherwise = 10 ^ (n - 2) + countIJ' (n - 1) i j
	where
		k = if i == j then 1 else 2

hdigit :: Int -> Int
hdigit n
	| n <= 9    = n
	| otherwise = hdigit (n `div` 10)

mdigits :: Int -> Int
mdigits n = n `mod` (10 ^ (ndig - 1)) `div` 10
	where
		ndig = ndigit n

tdigit :: Int -> Int
tdigit = (`mod` 10)

countIJ :: Int -> Int -> Int -> Int
countIJ n i j
 	| h < i     = low
	| h == i    = mdigits n + (if j <= tdigit n then 1 else 0) + low
	| otherwise = countIJ' (ndigit n) i j
	where
		h = hdigit n
		mid = mdigits n
		low = countIJ' (ndigit n - 1) i j

countIJRef :: Int -> Int -> Int -> Int
countIJRef n i j = length [x | x <- [1..n], check x i j]
	where
		check n i j = hdigit n == i && tdigit n == j

ans :: Int -> Int
ans n = sum $ map (uncurry cnt) pairs
	where
		pairs = (,) <$> [1..9] <*> [1..9]
		cs = Array.array ((1, 1), (9, 9)) [((i, j), countIJ n i j) | i <- [1..9], j <- [1..9]]
		cnt i j = (cs IArray.! (i, j)) * (cs IArray.! (j, i))

main :: IO ()
main = do
	n <- readLn
	print $ ans n