import java.util.*; 

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

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