// Arup Guha
// 10/22/2024
// Code to Explore Modular Exponentiation, Primitive Roots and the Discrete Log Problem

import java.util.*;

public class disclog {

	final public static int P = 29;
	
	public static void main(String[] args) {
		
		// Count how many items have a particular cycle size.
		int[] freq = new int[P+1];
	
		// Column labels.
		for (int label=0; label<P; label++)
			System.out.printf("%4d", label);
		System.out.println();
		System.out.println("-------------------------------------------------------");
	
		// Use base as the base for the exponentiation.
		for (int base=1; base<P; base++) {
		
			// Stores base to the power exp mod P.
			int ans = 1;
			
			// Marks if we've seen a cycle.
			boolean flag = false;
			
			// exp is the power we're raising base to...
			for (int exp=0; exp<P; exp++) {
				
				// Print and update ans.
				System.out.printf("%4d", ans);
				ans = (ans*base)%P;
				
				// Hit a cycle for the first time mark it.
				if (ans == 1 && !flag) {
					freq[exp+1]++;
					flag = true;
				}
			}
			System.out.println();
		
		}
		
		// Print out how many of each cycle size there is.
		for (int i=1; i<=P; i++) {
			if (freq[i] > 0)
				System.out.println(i+"\t"+freq[i]);
		}
	
	}

}