#include <bits/stdc++.h>

using namespace std;

#define ll long long
ll input(){
	ll x=0,f=0;char ch=getchar();
	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return f? -x:x;
}

ll t;
ll Ans=0;
ll t3[50],sum[50];
int main(){
	t=input()+1;
	t3[0]=1,sum[0]=1;
	for(int i=1;i<40;i++){
		t3[i]=t3[i-1]*3;
		sum[i]=t3[i]+sum[i-1];
	}
	ll x=1,tt=0;
	while(x<t){
		Ans++;
		if(x+sum[tt+1]<=t){
			tt++;
			x+=t3[tt];
		}else if(x+sum[tt]<=t){
			x+=t3[tt];
		}else{
			tt--;
			x+=t3[tt];
		}
	}
	printf("%lld\n",Ans);
}
