import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		long ans = 0;
		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int a, int b) {
		int na = Math.max(a, b);
		int nb = Math.min(a, b);
		if(na % nb != 0) {
			return gcd(nb, na % nb);
		}else return nb;
	}
}  import java.util.*; util.* java import java.math.*; math.* java public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		long ans = 0;
		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int a, int b) {
		int na = Math.max(a, b);
		int nb = Math.min(a, b);
		if(na % nb != 0) {
			return gcd(nb, na % nb);
		}else return nb;
	}
} Main public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		long ans = 0;
		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		}
		System.out.println(ans);
	} main {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		long ans = 0;
		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		}
		System.out.println(ans);
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int k = in.nextInt(); k in.nextInt() in.nextInt in long ans = 0; ans 0 for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		} int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		} {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		} for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			} {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			} for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				} int l = 1; int l = 1; l 1 l <= k l k l++ l++ l {
					ans += gcd(gcd(i, j), l);
				} {
					ans += gcd(gcd(i, j), l);
				} ans += gcd(gcd(i, j), l) ans gcd(gcd(i, j), l) gcd gcd(i, j) gcd i j l System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static int gcd(int a, int b) {
		int na = Math.max(a, b);
		int nb = Math.min(a, b);
		if(na % nb != 0) {
			return gcd(nb, na % nb);
		}else return nb;
	} gcd {
		int na = Math.max(a, b);
		int nb = Math.min(a, b);
		if(na % nb != 0) {
			return gcd(nb, na % nb);
		}else return nb;
	} int na = Math.max(a, b); na Math.max(a, b) Math.max Math a b int nb = Math.min(a, b); nb Math.min(a, b) Math.min Math a b if(na % nb != 0) {
			return gcd(nb, na % nb);
		}else return nb; na % nb != 0 na % nb na nb 0 {
			return gcd(nb, na % nb);
		} return gcd(nb, na % nb); gcd(nb, na % nb) gcd nb na % nb na nb return nb; nb int a a int b b public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		long ans = 0;
		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int a, int b) {
		int na = Math.max(a, b);
		int nb = Math.min(a, b);
		if(na % nb != 0) {
			return gcd(nb, na % nb);
		}else return nb;
	}
} public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		long ans = 0;
		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int a, int b) {
		int na = Math.max(a, b);
		int nb = Math.min(a, b);
		if(na % nb != 0) {
			return gcd(nb, na % nb);
		}else return nb;
	}
} Main
import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();
		long ans = 0;
		for(int i = 1; i <= k; i++) {
			for(int j = 1; j <= k; j++) {
				for(int l = 1; l <= k; l++) {
					ans += gcd(gcd(i, j), l);
				}
			}
		}
		System.out.println(ans);
	}
	public static int gcd(int a, int b) {
		int na = Math.max(a, b);
		int nb = Math.min(a, b);
		if(na % nb != 0) {
			return gcd(nb, na % nb);
		}else return nb;
	}
} 
