import math from collections import defaultdict n = int(input()) k = defaultdict(lambda:-1) a = [] kuri = -1 offset = -1 i = 1 j = 1 for cnt in range(1010): if k[(i, j)] >= 0: offset = k[(i, j)] kuri = cnt - k[(i, j)] break a.append((i, j)) k[(i, j)] = cnt i, j = (i+3*j)%1000, (i+j)%1000 #print(a) #print(offset, kuri) if n == 0: print(1) elif n-1 < offset: if n % 2 == 1: print(a[n-1][0]*2%1000) else: print((a[n-1][0]*2 - 1)%1000) else: targ = (n-1-offset) % kuri if n % 2 == 1: print(a[targ+offset][0]*2%1000) else: print((a[targ+offset][0]*2 - 1)%1000)