#include<stdio.h>
int gcd(int a, int b){
    int r, tmp;
  /* 自然数 a > b を確認・入替 */
    if(a < b){
        tmp = a;
        a = b;
        b = tmp;
    }
  /* ユークリッドの互除法 */
    r = a % b;
    while(r!=0){
        a = b;
        b = r;
        r = a % b;
    }

    return b;
}

int main(){
    int k;
    int sum = 0;
    scanf("%d", &k);
    for(int a = 1; a <= k; a++){
        for(int b = 1; b <= k; b++){
            for(int c = 1; c <= k; c++){
                sum = sum + gcd(a, gcd(b, c));
            }
        }
    }
    printf("%d", sum);
    return 0;
}
