package it.simone.google.code.jam2012;

import java.util.HashSet;
import java.util.Set;

public class RecycledNumber implements GoogleCodeExercise {

	int a = 0;
	int b = 0;
	Set<Couple> distinctCouple = null;
	long maxTime = 0;

	private class Couple {
		int x = 0;
		int y = 0;

		public Couple(int x, int y) {
			this.x = x;
			this.y = y;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + x;
			result = prime * result + y;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Couple other = (Couple) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (x == other.x && y == other.y)
				return true;
			if (x == other.y && y == other.x)
				return true;
			return false;
		}

		private RecycledNumber getOuterType() {
			return RecycledNumber.this;
		}

	}

	@Override
	public String execute(String line) {
		int result = 0;

		String[] data = line.split(" ");
		a = Integer.parseInt(data[0]);
		b = Integer.parseInt(data[1]);
		initialize();

		int n = a;
		//
		while (n <= b) {
			result += analize(n);
			n++;
//			if (n % 1000 == 0)
//				System.out.println(n);
		}

		System.out.println(result);
		return "" + result;
	}

	@Override
	public void initialize() {
		distinctCouple = new HashSet<Couple>();
	}

	private int analize(int number) {
		String numString = "" + number;
		initialize();
		for (int i = 1; i < numString.length(); i++) {
			int m = shift(numString, i);
			if (a <= number && number < m && m <= b) {
				Couple couple=new Couple(number,m);
				if (!distinctCouple.contains(couple))
					distinctCouple.add(couple);
			}
		}
		return distinctCouple.size();
	}

	private int shift(String numString, int posx) {

		String result = null;
		result = "" + numString.substring(posx) + numString.substring(0, posx);

		return Integer.parseInt(result.toString());
	}

}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ale
 */
public class RecycledNumbers {
    public static int[][] parseInput(String path) throws FileNotFoundException, IOException{
        BufferedReader reader = new BufferedReader(new FileReader(path));
        int size = Integer.parseInt(reader.readLine());
        int[][] result = new int[size][2];
        for (int i = 0; i < size; i++){
            String[] nums = reader.readLine().split(" ");
            result[i][0] = Integer.parseInt(nums[0]);
            result[i][1] = Integer.parseInt(nums[1]);
        }
        return result;
    }

// somebody has to explain me why it doesn't work
//    public static int solveInstance(int N, int M){
//        boolean[] done = new boolean[M+1];
//        int total = 0;
//        int D = (""+N).length() -1;
//        int divisor = (int) Math.round(Math.pow(10, D));
//        for (int i = 0; i < N; i++){
//            done[i] = true;
//        }
//        for (int i = N; i <= M; i++){
//            done[i] = false;
//        }
//        for (int i = N; i <= M; i++){
//            if (!done[i]){
//                done[i] = true;
//                int rotation = i;
//                int accepted = 1;
//                for (int j = 0; j < D; j++){
//                    int msd = rotation/divisor;
//                    rotation = (rotation - divisor*msd)*10 + msd;
//                    if (N <= rotation && rotation <= M && !done[rotation]){
////                        if (accepted == 1){System.out.print("\n@@@@@@@@@@\n"+i + " ");}
////                        System.out.print(rotation+" ");
//                        done[rotation] = true;
//                        accepted ++;
//                    }
//
//                }
//                total += accepted*(accepted-1)/2;
//            }
//        }
//        return total;
//    }

    // somebody has to explain me why it doesn't work
    public static int solveInstance(int N, int M){
        HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
        int total = 0;
        int D = (""+N).length() -1;
        int divisor = (int) Math.round(Math.pow(10, D));

        for (int i = N; i <= M; i++){
            int rotation = i;
            int minimum = i;
            for (int j = 0; j < D; j++){
                int msd = rotation/divisor;
                rotation = (rotation - divisor*msd)*10 + msd;
                if (rotation < minimum){
//                        if (accepted == 1){System.out.print("\n@@@@@@@@@@\n"+i + " ");}
//                        System.out.print(rotation+" ");

                    minimum = rotation;
                }
            }
            Integer previous = map.put(minimum, 1);
            if (previous != null){
                map.put(minimum, (previous + 1));
            }
        }
        for (Integer i : map.values()){
            total += (i*(i-1))/2;
        }
        return total;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException{
        String dir = "C:/Users/ale/Desktop/GoogleCodeJam/";
        String[] files = new File(dir).list();
        int[][] instances = parseInput(dir+files[0]);
        //parseInput("C:/Users/ale/Desktop/GoogleCodeJam/C-small-attempt0.in");
        PrintStream writer = new PrintStream(new File("C:/Users/ale/workspace/GoogleCodeJam/src/output.out"));


        for (int i = 0; i < instances.length; i++){
            int sol = solveInstance(instances[i][0], instances[i][1]);
            writer.println("Case #"+(i+1)+": "+sol);
        }
        writer.close();
    }

}
