import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
	int K = Integer.parseInt(sc.next());
	int 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.print(ans);
    return;
  }

  static int gcd(int a, int b){
    int max= Math.max(a,b);
    int min= Math.min(a,b);
    if(max%min==0){
      return min;
    }else{
      return gcd(max%min, min);
    }
  }  
  
}

 import java.util.Scanner; Scanner java.util public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
	int K = Integer.parseInt(sc.next());
	int 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.print(ans);
    return;
  }

  static int gcd(int a, int b){
    int max= Math.max(a,b);
    int min= Math.min(a,b);
    if(max%min==0){
      return min;
    }else{
      return gcd(max%min, min);
    }
  }  
  
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
	int K = Integer.parseInt(sc.next());
	int 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.print(ans);
    return;
  } main {
    Scanner sc = new Scanner(System.in);
	int K = Integer.parseInt(sc.next());
	int 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.print(ans);
    return;
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int 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.print(ans) System.out.print System.out System System.out ans return; String[] args args static int gcd(int a, int b){
    int max= Math.max(a,b);
    int min= Math.min(a,b);
    if(max%min==0){
      return min;
    }else{
      return gcd(max%min, min);
    }
  } gcd {
    int max= Math.max(a,b);
    int min= Math.min(a,b);
    if(max%min==0){
      return min;
    }else{
      return gcd(max%min, min);
    }
  } int max= Math.max(a,b); max Math.max(a,b) Math.max Math a b int min= Math.min(a,b); min Math.min(a,b) Math.min Math a b if(max%min==0){
      return min;
    }else{
      return gcd(max%min, min);
    } max%min==0 max%min max min 0 {
      return min;
    } return min; min {
      return gcd(max%min, min);
    } return gcd(max%min, min); gcd(max%min, min) gcd max%min max min min int a a int b b public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
	int K = Integer.parseInt(sc.next());
	int 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.print(ans);
    return;
  }

  static int gcd(int a, int b){
    int max= Math.max(a,b);
    int min= Math.min(a,b);
    if(max%min==0){
      return min;
    }else{
      return gcd(max%min, min);
    }
  }  
  
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
	int K = Integer.parseInt(sc.next());
	int 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.print(ans);
    return;
  }

  static int gcd(int a, int b){
    int max= Math.max(a,b);
    int min= Math.min(a,b);
    if(max%min==0){
      return min;
    }else{
      return gcd(max%min, min);
    }
  }  
  
} Main
import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
	int K = Integer.parseInt(sc.next());
	int 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.print(ans);
    return;
  }

  static int gcd(int a, int b){
    int max= Math.max(a,b);
    int min= Math.min(a,b);
    if(max%min==0){
      return min;
    }else{
      return gcd(max%min, min);
    }
  }  
  
}


