def solver(): M = int(raw_input()) X = 2017 print (X + pow(X*X,X,M)) % M if __name__ == "__main__": solver()