def main():

  import math

  N = int(eval(input()))

  ans=0

  for i in range(1,N+1):

    for j in range(1, N+1):

      for k in range(1, N+1):

          t=math.gcd(i,j)

          t=math.gcd(t,k)

          ans+=t

        

  print(ans)



if __name__=='__main__':

  main()

def main():

  import math

  N = int(eval(input()))

  gcd={}

  ans=0

  for i in range(1,N+1):

    for j in range(1, N+1):

      key=i*N+j

      gcd[key]=math.gcd(i,j)

  

  for k, i in list(gcd.items()):

    for j in range(1, N+1):

      key=j*N+i

      ans+=gcd[key]

        

  print(ans)



if __name__=='__main__':

  main()

def main():
    import math

    N = int(eval(input()))
    ans = 0
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            for k in range(1, N + 1):
                t = math.gcd(i, j)
                t = math.gcd(t, k)
                ans += t
    print(ans)


if __name__ == "__main__":
    main()

def main():
    import math

    N = int(eval(input()))
    gcd = {}
    ans = 0
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            key = i * N + j
            gcd[key] = math.gcd(i, j)
    for k, i in list(gcd.items()):
        for j in range(1, N + 1):
            key = j * N + i
            ans += gcd[key]
    print(ans)


if __name__ == "__main__":
    main()

+    gcd = {} -            for k in range(1, N + 1): -                t = math.gcd(i, j) -                t = math.gcd(t, k) -                ans += t +            key = i * N + j +            gcd[key] = math.gcd(i, j) +    for k, i in list(gcd.items()): +        for j in range(1, N + 1): +            key = j * N + i +            ans += gcd[key]