import java.util.*;
public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int a,b,c;
		int output = 0;
		for(a = 1;a<=num;a++)
			for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c);
		System.out.println(output);
	}
	
	public static int gcd(int p, int q)
	{
		if(q == 0)
		{
			return p;
		}
		return gcd(q,p%q);
	}

} import java.util.*; util.* java public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int a,b,c;
		int output = 0;
		for(a = 1;a<=num;a++)
			for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c);
		System.out.println(output);
	}
	
	public static int gcd(int p, int q)
	{
		if(q == 0)
		{
			return p;
		}
		return gcd(q,p%q);
	}

} Main public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int a,b,c;
		int output = 0;
		for(a = 1;a<=num;a++)
			for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c);
		System.out.println(output);
	} main {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int a,b,c;
		int output = 0;
		for(a = 1;a<=num;a++)
			for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c);
		System.out.println(output);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int num = sc.nextInt(); num sc.nextInt() sc.nextInt sc int a a b b c; c int output = 0; output 0 for(a = 1;a<=num;a++)
			for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c); a = 1; a = 1 a 1 a<=num a num a++ a++ a for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c); for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c); b = 1; b = 1 b 1 b<=num b num b++ b++ b for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c); for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c); c = 1; c = 1 c 1 c<=num c num c++ c++ c output += gcd(gcd(a,b),c); output += gcd(gcd(a,b),c) output gcd(gcd(a,b),c) gcd gcd(a,b) gcd a b c System.out.println(output) System.out.println System.out System System.out output String[] args args public static int gcd(int p, int q)
	{
		if(q == 0)
		{
			return p;
		}
		return gcd(q,p%q);
	} gcd {
		if(q == 0)
		{
			return p;
		}
		return gcd(q,p%q);
	} if(q == 0)
		{
			return p;
		} q == 0 q 0 {
			return p;
		} return p; p return gcd(q,p%q); gcd(q,p%q) gcd q p%q p q int p p int q q public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int a,b,c;
		int output = 0;
		for(a = 1;a<=num;a++)
			for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c);
		System.out.println(output);
	}
	
	public static int gcd(int p, int q)
	{
		if(q == 0)
		{
			return p;
		}
		return gcd(q,p%q);
	}

} public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int a,b,c;
		int output = 0;
		for(a = 1;a<=num;a++)
			for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c);
		System.out.println(output);
	}
	
	public static int gcd(int p, int q)
	{
		if(q == 0)
		{
			return p;
		}
		return gcd(q,p%q);
	}

} Main
import java.util.*;
public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int a,b,c;
		int output = 0;
		for(a = 1;a<=num;a++)
			for(b = 1;b<=num;b++)
				for(c = 1;c<=num;c++)
					output += gcd(gcd(a,b),c);
		System.out.println(output);
	}
	
	public static int gcd(int p, int q)
	{
		if(q == 0)
		{
			return p;
		}
		return gcd(q,p%q);
	}

}