H Happy Number

#include<iostream>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>

#define fi first
#define se second

#define lowbit(x) (x&-x)

using namespace std;

namespace ae86{
    const int bufl=1<<15;
    char buf[bufl],*s=buf,*t=buf;
    inline int fetch(){
        if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
        return*s++;
    }
    inline int read(){
        int a=0,b=1,c=fetch();
        while(!isdigit(c))b^=c=='-',c=fetch();
        while(isdigit(c))a=a*10+c-48,c=fetch();
        return b?a:-a;
    }
}
using ae86::read;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,double> pid;

const int inf=0x3f3f3f3f;
const ll INF=2e18;
const double eps=1e-8;
const double pi=acos(-1);
const int mod=1e9+7;

int n;
ll inv[25];
char s[]="236";
string ans="";

void solve(ll x,int l){
    for(int i=l;i>=1;i--){
        ll t=x/inv[i-1];
        if(x%inv[i-1]==0) t--;
        ans+=s[t];
        x-=t*inv[i-1];
    }
}

void init(){
    inv[0]=1;
    for(int i=1;i<=24;i++)    inv[i]=inv[i-1]*3;
}

int main(){
    scanf("%d",&n);
    init();
    ll res=0;
    ll len;
    for(len=1;res<n;len++)    res+=inv[len];
    ll tmp=res-inv[len-1];
    ll lst=n-tmp;
    // cout<<len-1<<'\n'<<lst<<'\n';
    solve(lst,len-1);
    cout<<ans;
}