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

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;


public class Case implements Runnable {

	private int id;
	private String output;

	private int a;
	private int b;
	private String sa;
	private String sb;
	private ArrayList<Couple> hs = new ArrayList<Couple>();

	public Case(int id, int a, int b) {
		super();
		this.id = id;
		this.a = a;
		this.b = b;
	}

	public String solve() {
		int l = (int) (Math.log10(a)) + 1;
		for (int i = a; i <= b; i++) {
			for (int j : shifts(i)) {
				if (i != j && j >= a && j <= b && (((int) (Math.log10(j)) + 1) == l)) {
					Couple c = new Couple(i, j);
					if (!hs.contains(c))
						hs.add(c);
				}
			}
		}
		return String.valueOf(hs.size());
	}

	private int[] shifts(int i) {
		int l = (int) (Math.log10(i)) + 1;
		int[] dgt = new int[l];
		int[] shf = new int[l-1];
		int j = i;
		for (int k = l; k > 0; k--) {
			dgt[l-k] = j / pow(k-1);
			j = j % pow(k-1);
		}
		for (int k = 1; k < l; k++) {
			int x = 0;
			for (int h = 0; h < l; h++) {
				x = 10 * x + dgt[(k+h)%l];
			}
			shf[k-1] = x;
		}
		return shf;
	}

	private int pow(int n) {
		switch(n) {
		case 0:
			return 1;
		case 1:
			return 10;
		case 2:
			return 100;
		case 3:
			return 1000;
		case 4:
			return 10000;
		case 5:
			return 100000;
		case 6:
			return 1000000;
		case 7:
			return 10000000;
		case 8:
			return 100000000;
		case 9:
			return 1000000000;
		}
		return 0;
	}

	private int pow99(int n) {
		int k = 1;
		int pow = 9;
		for (int i = 1; i < n; i++)
			pow = pow * 10 + 9;
		return pow;
	}

	@Override
	public void run() {
		output = solve();
	}

	@Override
	public String toString() {
		return "Case #" + id + ": " + output;
	}

}