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());
	}

}

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

import dancing.gogglers.Googlers;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

/**
 *
 * @author Raveesh
 */
public class Recycler {
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        String filePath = "C:\\Users\\Raveesh\\Downloads\\C-large.in";
        String filePathOutput = filePath + "_out";
        BufferedReader stdin = new BufferedReader(new FileReader(new File(filePath)));
        FileWriter fileWriter = new FileWriter(filePathOutput);
        ArrayList stringList = Recycler.getInputList(stdin);
        int count = 1;
        Iterator iter = stringList.iterator();
        while (iter.hasNext()) {
            String inputStr = (String) iter.next();
            String[] inputStrArr = inputStr.split("\\ ");
            int startNum  = Integer.valueOf(inputStrArr[0]);
            int endNum  = Integer.valueOf(inputStrArr[1]);
            int output = Recycler.performPerm(startNum,endNum);
            String out = "Case #" + count + ": " + output;
            fileWriter.write(out);
            fileWriter.write("\n");
            count++;
        }       
        fileWriter.close();

    }
    
    private static int performPerm(int start,int end ) {
        finalSet = new ArrayList();
        inputSet = new HashSet();
        for (int i=start;i<=end;i++) {
//            System.out.println("Running" + i);
            StringBuffer val = new StringBuffer(String.valueOf(i));
//            System.out.println("Running" + val);
            checkVal(start, end, val);
        }
        
//        Iterator iter = finalSet.iterator();
//        while (iter.hasNext()) {
//            System.out.println("Val " + iter.ne);
//        } 
//        System.out.println("=========================COUNT " + finalSet.size()/2);
        return finalSet.size()/2;
        
        
    }
    
    private static ArrayList finalSet = new ArrayList();
    private static HashSet inputSet = new HashSet();
    private static boolean checkVal (int start,int end, StringBuffer val) {
        
        
        for (int i =0;i< val.length() ; i++) {
            String temp = val.substring(0,i+1);
            String rem = val.substring(i+1,val.length());
            String val1 = rem + temp;
//            System.out.println("temp" + temp + " rem : " + rem);
//            for (int j = 0;j <= rem.length() ; j++) {
//                StringBuffer val1 = new StringBuffer(rem);
                
                
//                if (j==i) {
//                    continue;
//                    
//                }
//                val1.insert(j, temp);
//                System.out.println("iNPUT :; " + val + " :: Output : " + val1);
//                System.out.println("iNPUT :; " + val + " :: Output : " + val1);
                int t = Integer.valueOf(val1.toString());
                if (t >= start && t <= end && !val.toString().equals(val1.toString())) {
//                    System.out.println("iNPUT :; " + val + " :: Output : " + val1 + " t :: " + t );
                    if (inputSet.add(val + "," + val1)) {
                        finalSet.add(t);
                    }
////                    System.out.println("dding :: " + t);
//                    String key = val.toString() + ","  + val1.toString();
//                    String key1 = val1.toString() + "," + val.toString();
//                    if(!(finalSet.containsKey(key)|| finalSet.containsKey(key1))) {
                        
//                        finalSet.put(key, t);
//                    }
////                    finalSet.put(Integer.valueOf(val1.toString()));
//                }
//                System.out.println("num :: " + val1);
            }
        }
        return true;
    }
    
    
    private static ArrayList getInputList(BufferedReader stdin) throws IOException {
        ArrayList stringList = new ArrayList<String>();
        String line;
        int inputLineCount = 0;
        Integer testCaseCount = 0;
        while ((line = stdin.readLine()) != null && line.length() != 0 && inputLineCount <= testCaseCount.intValue()) {
//            System.out.println("line ::  " + line);
            if (inputLineCount == 0) {
                try {
                    testCaseCount = Integer.valueOf(line);
                } catch (NumberFormatException pe) {
                    System.out.println("Invalid input for # of test cases");
                    System.exit(0);
                }
                inputLineCount++;
                continue;
            }
            stringList.add(line);
            inputLineCount++;

            if (inputLineCount == testCaseCount + 1) {
                break;
            }

        }
        return stringList;
    }
    
}
