import java.util.*;
class Main{
	public static void main(String[] args){
    	Scanner scanner = new Scanner(System.in);
        int n = (int) scanner.nextInt();
        int ans = 0;
      	for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        }
        }
      System.out.println(ans);
      
    }
  	
  	static int gcd(int a, int b){
    	while(a != b){
        	if (a >= b){
            	a-=b;
            }else{
            	b-=a;
            }
        }
      return a;
    }
} import java.util.*; util.* java class Main{
	public static void main(String[] args){
    	Scanner scanner = new Scanner(System.in);
        int n = (int) scanner.nextInt();
        int ans = 0;
      	for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        }
        }
      System.out.println(ans);
      
    }
  	
  	static int gcd(int a, int b){
    	while(a != b){
        	if (a >= b){
            	a-=b;
            }else{
            	b-=a;
            }
        }
      return a;
    }
} Main public static void main(String[] args){
    	Scanner scanner = new Scanner(System.in);
        int n = (int) scanner.nextInt();
        int ans = 0;
      	for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        }
        }
      System.out.println(ans);
      
    } main {
    	Scanner scanner = new Scanner(System.in);
        int n = (int) scanner.nextInt();
        int ans = 0;
      	for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        }
        }
      System.out.println(ans);
      
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = (int) scanner.nextInt(); n (int) scanner.nextInt() scanner.nextInt scanner int ans = 0; ans 0 for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        }
        } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
        for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        }
        } {
        for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        }
        } for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        } int j = 1; int j = 1; j 1 j <= n j n j++ j++ j {
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        } {
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        } for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        } int k = 1; int k = 1; k 1 k <= n k n k++ k++ k {
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        } {
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        } int c = gcd(i, j); c gcd(i, j) gcd i j ans+= gcd(k, c) ans gcd(k, c) gcd k c System.out.println(ans) System.out.println System.out System System.out ans String[] args args static int gcd(int a, int b){
    	while(a != b){
        	if (a >= b){
            	a-=b;
            }else{
            	b-=a;
            }
        }
      return a;
    } gcd {
    	while(a != b){
        	if (a >= b){
            	a-=b;
            }else{
            	b-=a;
            }
        }
      return a;
    } while(a != b){
        	if (a >= b){
            	a-=b;
            }else{
            	b-=a;
            }
        } a != b a b {
        	if (a >= b){
            	a-=b;
            }else{
            	b-=a;
            }
        } if (a >= b){
            	a-=b;
            }else{
            	b-=a;
            } a >= b a b {
            	a-=b;
            } a-=b a b {
            	b-=a;
            } b-=a b a return a; a int a a int b b
import java.util.*;
class Main{
	public static void main(String[] args){
    	Scanner scanner = new Scanner(System.in);
        int n = (int) scanner.nextInt();
        int ans = 0;
      	for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
        for (int k = 1; k <= n; k++){
          int c = gcd(i, j);		
          ans+= gcd(k, c);
        }
        }
        }
      System.out.println(ans);
      
    }
  	
  	static int gcd(int a, int b){
    	while(a != b){
        	if (a >= b){
            	a-=b;
            }else{
            	b-=a;
            }
        }
      return a;
    }
}