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

  private static int gcd (int a, int b) {
    if (b > a) {
      int x = a;
      a = b;
      b = x;
    }
    
    int temp;
    while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
  }

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

  private static int gcd (int a, int b) {
    if (b > a) {
      int x = a;
      a = b;
      b = x;
    }
    
    int temp;
    while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
  }

} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long ans = 0;
    for (int a = 1; a <= k; a++) {
      for (int b = 1; b <= k; b++) {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      }
    }
 
    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    long ans = 0;
    for (int a = 1; a <= k; a++) {
      for (int b = 1; b <= k; b++) {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      }
    }
 
    System.out.println(ans);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc long ans = 0; ans 0 for (int a = 1; a <= k; a++) {
      for (int b = 1; b <= k; b++) {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      }
    } int a = 1; int a = 1; a 1 a <= k a k a++ a++ a {
      for (int b = 1; b <= k; b++) {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      }
    } {
      for (int b = 1; b <= k; b++) {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      }
    } for (int b = 1; b <= k; b++) {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      } int b = 1; int b = 1; b 1 b <= k b k b++ b++ b {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      } {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      } for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        } int c= 1; int c= 1; c 1 c <= k c k c++ c++ c {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        } {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        } int temp = Main.gcd(a, b); temp Main.gcd(a, b) Main.gcd Main a b ans += Main.gcd(temp, c) ans Main.gcd(temp, c) Main.gcd Main temp c System.out.println(ans) System.out.println System.out System System.out ans String[] args args private static int gcd (int a, int b) {
    if (b > a) {
      int x = a;
      a = b;
      b = x;
    }
    
    int temp;
    while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
  } gcd {
    if (b > a) {
      int x = a;
      a = b;
      b = x;
    }
    
    int temp;
    while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
  } if (b > a) {
      int x = a;
      a = b;
      b = x;
    } b > a b a {
      int x = a;
      a = b;
      b = x;
    } int x = a; x a a = b a b b = x b x int temp; temp while((temp = a%b)!=0) {
		a = b;
		b = temp;
	} (temp = a%b)!=0 (temp = a%b) temp a%b a b 0 {
		a = b;
		b = temp;
	} a = b a b b = temp b temp return b; b 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();
    long ans = 0;
    for (int a = 1; a <= k; a++) {
      for (int b = 1; b <= k; b++) {
        for (int c= 1; c <= k; c++) {
          int temp = Main.gcd(a, b);
          ans += Main.gcd(temp, c);
        }
      }
    }
 
    System.out.println(ans);
  }

  private static int gcd (int a, int b) {
    if (b > a) {
      int x = a;
      a = b;
      b = x;
    }
    
    int temp;
    while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
  }

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

  private static int gcd (int a, int b) {
    if (b > a) {
      int x = a;
      a = b;
      b = x;
    }
    
    int temp;
    while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
  }

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

  private static int gcd (int a, int b) {
    if (b > a) {
      int x = a;
      a = b;
      b = x;
    }
    
    int temp;
    while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return b;
  }

}