import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main 
{
	static long gcd(long a, long b) 
    { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } 
	
	static long GCD(long A, long B, long C)
	{
		long Result = A;
		Result = gcd(Result, B);
		Result = gcd(Result, C);
		return Result;
	}
	public static void main(String[] Arguments) throws IOException 
	{
		BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
		long K = Long.parseLong(BR.readLine()), Sum = 0;
		for (long i = 1; i <= K; i++)
			for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k);
		System.out.println(Sum);
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main 
{
	static long gcd(long a, long b) 
    { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } 
	
	static long GCD(long A, long B, long C)
	{
		long Result = A;
		Result = gcd(Result, B);
		Result = gcd(Result, C);
		return Result;
	}
	public static void main(String[] Arguments) throws IOException 
	{
		BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
		long K = Long.parseLong(BR.readLine()), Sum = 0;
		for (long i = 1; i <= K; i++)
			for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k);
		System.out.println(Sum);
	}
} Main static long gcd(long a, long b) 
    { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } gcd { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } if (a == 0) 
            return b; a == 0 a 0 return b; b return gcd(b % a, a); gcd(b % a, a) gcd b % a b a a long a a long b b static long GCD(long A, long B, long C)
	{
		long Result = A;
		Result = gcd(Result, B);
		Result = gcd(Result, C);
		return Result;
	} GCD {
		long Result = A;
		Result = gcd(Result, B);
		Result = gcd(Result, C);
		return Result;
	} long Result = A; Result A Result = gcd(Result, B) Result gcd(Result, B) gcd Result B Result = gcd(Result, C) Result gcd(Result, C) gcd Result C return Result; Result long A A long B B long C C public static void main(String[] Arguments) throws IOException 
	{
		BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
		long K = Long.parseLong(BR.readLine()), Sum = 0;
		for (long i = 1; i <= K; i++)
			for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k);
		System.out.println(Sum);
	} main {
		BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
		long K = Long.parseLong(BR.readLine()), Sum = 0;
		for (long i = 1; i <= K; i++)
			for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k);
		System.out.println(Sum);
	} BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); BR new BufferedReader(new InputStreamReader(System.in)) long K = Long.parseLong(BR.readLine()) K Long.parseLong(BR.readLine()) Long.parseLong Long BR.readLine() BR.readLine BR Sum = 0; Sum 0 for (long i = 1; i <= K; i++)
			for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k); long i = 1; long i = 1; i 1 i <= K i K i++ i++ i for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k); for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k); long j = 1; long j = 1; j 1 j <= K j K j++ j++ j for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k); for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k); long k = 1; long k = 1; k 1 k <= K k K k++ k++ k Sum += GCD(i, j, k); Sum += GCD(i, j, k) Sum GCD(i, j, k) GCD i j k System.out.println(Sum) System.out.println System.out System System.out Sum String[] Arguments Arguments public class Main 
{
	static long gcd(long a, long b) 
    { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } 
	
	static long GCD(long A, long B, long C)
	{
		long Result = A;
		Result = gcd(Result, B);
		Result = gcd(Result, C);
		return Result;
	}
	public static void main(String[] Arguments) throws IOException 
	{
		BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
		long K = Long.parseLong(BR.readLine()), Sum = 0;
		for (long i = 1; i <= K; i++)
			for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k);
		System.out.println(Sum);
	}
} public class Main 
{
	static long gcd(long a, long b) 
    { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } 
	
	static long GCD(long A, long B, long C)
	{
		long Result = A;
		Result = gcd(Result, B);
		Result = gcd(Result, C);
		return Result;
	}
	public static void main(String[] Arguments) throws IOException 
	{
		BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
		long K = Long.parseLong(BR.readLine()), Sum = 0;
		for (long i = 1; i <= K; i++)
			for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k);
		System.out.println(Sum);
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main 
{
	static long gcd(long a, long b) 
    { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } 
	
	static long GCD(long A, long B, long C)
	{
		long Result = A;
		Result = gcd(Result, B);
		Result = gcd(Result, C);
		return Result;
	}
	public static void main(String[] Arguments) throws IOException 
	{
		BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
		long K = Long.parseLong(BR.readLine()), Sum = 0;
		for (long i = 1; i <= K; i++)
			for (long j = 1; j <= K; j++)
				for (long k = 1; k <= K; k++)
					Sum += GCD(i, j, k);
		System.out.println(Sum);
	}
}
