import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
public class Main {
  public static void main(String[] args){
        
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int cnt = 0;
    for (int i = 1; i < K+1; i++) {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    }
    System.out.println(cnt);
    
   
    sc.close();
  }
  static int gcd (int a, int b, int c){
    int result = gcd(a, b);
    return gcd(c, result);
  }
  static int gcd (int a, int b) {
    return b>0?gcd(b,a%b):a;
  }

} import java.util.*; util.* java import java.lang.*; lang.* java import java.math.*; math.* java import java.io.*; io.* java public class Main {
  public static void main(String[] args){
        
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int cnt = 0;
    for (int i = 1; i < K+1; i++) {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    }
    System.out.println(cnt);
    
   
    sc.close();
  }
  static int gcd (int a, int b, int c){
    int result = gcd(a, b);
    return gcd(c, result);
  }
  static int gcd (int a, int b) {
    return b>0?gcd(b,a%b):a;
  }

} Main public static void main(String[] args){
        
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int cnt = 0;
    for (int i = 1; i < K+1; i++) {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    }
    System.out.println(cnt);
    
   
    sc.close();
  } main {
        
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int cnt = 0;
    for (int i = 1; i < K+1; i++) {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    }
    System.out.println(cnt);
    
   
    sc.close();
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int cnt = 0; cnt 0 for (int i = 1; i < K+1; i++) {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    } int i = 1; int i = 1; i 1 i < K+1 i K+1 K 1 i++ i++ i {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    } {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    } for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      } int j = 1; int j = 1; j 1 j < K+1 j K+1 K 1 j++ j++ j {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      } {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      } for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        } int j2 = 1; int j2 = 1; j2 1 j2 < K+1 j2 K+1 K 1 j2++ j2++ j2 {
          //gcd
          cnt += gcd(i, j, j2);
        } {
          //gcd
          cnt += gcd(i, j, j2);
        } cnt += gcd(i, j, j2) cnt gcd(i, j, j2) gcd i j j2 System.out.println(cnt) System.out.println System.out System System.out cnt sc.close() sc.close sc String[] args args static int gcd (int a, int b, int c){
    int result = gcd(a, b);
    return gcd(c, result);
  } gcd {
    int result = gcd(a, b);
    return gcd(c, result);
  } int result = gcd(a, b); result gcd(a, b) gcd a b return gcd(c, result); gcd(c, result) gcd c result int a a int b b int c c static int gcd (int a, int b) {
    return b>0?gcd(b,a%b):a;
  } gcd {
    return b>0?gcd(b,a%b):a;
  } return b>0?gcd(b,a%b):a; b>0?gcd(b,a%b):a b>0 b 0 gcd(b,a%b) gcd b a%b a b a int a a int b b public class Main {
  public static void main(String[] args){
        
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int cnt = 0;
    for (int i = 1; i < K+1; i++) {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    }
    System.out.println(cnt);
    
   
    sc.close();
  }
  static int gcd (int a, int b, int c){
    int result = gcd(a, b);
    return gcd(c, result);
  }
  static int gcd (int a, int b) {
    return b>0?gcd(b,a%b):a;
  }

} public class Main {
  public static void main(String[] args){
        
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int cnt = 0;
    for (int i = 1; i < K+1; i++) {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    }
    System.out.println(cnt);
    
   
    sc.close();
  }
  static int gcd (int a, int b, int c){
    int result = gcd(a, b);
    return gcd(c, result);
  }
  static int gcd (int a, int b) {
    return b>0?gcd(b,a%b):a;
  }

} Main
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
public class Main {
  public static void main(String[] args){
        
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int cnt = 0;
    for (int i = 1; i < K+1; i++) {
      for (int j = 1; j < K+1; j++) {
        for (int j2 = 1; j2 < K+1; j2++) {
          //gcd
          cnt += gcd(i, j, j2);
        }
      }
    }
    System.out.println(cnt);
    
   
    sc.close();
  }
  static int gcd (int a, int b, int c){
    int result = gcd(a, b);
    return gcd(c, result);
  }
  static int gcd (int a, int b) {
    return b>0?gcd(b,a%b):a;
  }

}