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

package recycled;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.TreeSet;

/**
 *
 * @author Alfred
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    try {
            FileInputStream fstream = new FileInputStream("C-small-attempt0.in");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            FileWriter outFile = new FileWriter("out.txt");
            PrintWriter out = new PrintWriter(outFile);
            String line;
            line = br.readLine();
            int cases = Integer.parseInt(line);
            for (int i=1; i<=cases;i++){
                line = br.readLine();
                String[] values = line.split(" ");
                int A = Integer.parseInt(values[0]);
                int B = Integer.parseInt(values[1]);
                int total=0;
                for (int n=A; n<=B;n++){
                    TreeSet<String> solutions = new TreeSet<String>();
                    for (int k=1;k<values[1].length();k++){
                        String m = circshift(n,k);
                        int mVal = Integer.parseInt(m);
                        if(mVal<=B && mVal!=n  && mVal> n && !m.startsWith("0") && m.length()==Integer.toString(n).length() )
                        {
                            solutions.add(m);
                        }                   
                    }
                    total+=solutions.size();
                }
                out.println("Case #"+i+": "+total);
            }
            in.close();
            out.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

}

    private static String circshift(int n, int k) {
        String nString=Integer.toString(n);
        int len=nString.length();
        String m =  nString.substring(len-k)+nString.subSequence(0, len-k);
        //System.out.println(n+" "+m);
        return m;
    }

}

package com.codejam2012;

import java.util.TreeMap;

import com.codejam2012.CodeJam1.Case;



public class CodeJam3 {

	public static String fileDir = "P:\\CodeJam2012\\";
	public static String baseFileName = "C-small-attempt2";
	public static String inputFileName = fileDir + baseFileName + ".in";
	public static String outputFileName = fileDir + baseFileName + ".out";

	public static int Tcase;
	public static int Ccase = 0;
	public static String[] cases;
	public static Case singleCase;
	
	public static void main(String[] args) {
		
		FileUtils.deleteFile(outputFileName);
		String inputString = FileUtils.readFile(inputFileName);
		parseInputString(inputString);
		for (; Ccase < Tcase;) {
			parse();
		}

	}
	
	public static void parseInputString(String inputString) {

		cases = inputString.split("\n");
		Tcase = Integer.parseInt(cases[0]);
		Ccase = 0;
	}

	private static void parse() {

		Ccase++;
		FileUtils.appendToFile(outputFileName, "Case #" + Ccase + ": ");
		singleCase = new Case(cases[Ccase]);
		singleCase.execute();
		FileUtils.appendToFile(outputFileName, singleCase.getDataToPrint()
				+ "\n");

	}

	public static class Case {
		StringBuilder outputStatement;
		
		int N;
		int M;

		public Case(String caseString) {
			outputStatement = new StringBuilder();
			String[] arr = caseString.split(" " );
			N = Integer.parseInt(arr[0]);
			M = Integer.parseInt(arr[1]);
		}

		public void execute() {
			
			int noOfDigits = (N + "").length();
			int TCount = 0;
			TreeMap<Integer, Integer> mList = new TreeMap<Integer,Integer>();
			

			for (int n = N; n <= M; n++) {

				for (int r = 1; r < noOfDigits; r++) {
					int m = rotater(n, r, noOfDigits);
					if (m <= M && m >= N) {
						if (m > n) {
							 if(mList.containsKey(n) ){
								 if(mList.get(n) == m){
									 
								 }else{
									 TCount++;
										mList.put(n,m);
								 }
							 }else if (mList.containsKey(m) ){
								 if(mList.get(m) == n){
									 
								 }else{
									 TCount++;
										mList.put(n,m);
								 }
							 }else{
								 TCount++;
									mList.put(n,m);
							 }
							
						}
					}

				}

			}
			
			outputStatement.append(TCount);
		}
		
		public static int rotater(int n, int rotate, int noOfDigits) {
			int base = (int) Math.pow(10, noOfDigits - 1);
			int[] digits = new int[noOfDigits];
			int d = 0;
			int ans = 0;
			for (int i = base; i >= 1; i /= 10) {
				digits[d] = n / i;
				d++;
				n = n % i;
			}

			shiftLeft(digits, rotate);
			d = 0;
			for (int i = base; i >= 1; i /= 10) {
				ans += digits[d] * i;
				d++;
			}

			return ans;

		}

		public static void shiftLeft(int[] array, int amount) {

			for (int j = 0; j < amount; j++) {
				int a = array[0];
				int i;
				for (i = 0; i < array.length - 1; i++)
					array[i] = array[i + 1];
				array[i] = a;
			}

		}

		public String getDataToPrint() {
			return outputStatement.toString();

		}

	}


}
