Problem:
Luggage
Mr. Yokohama is estimating the delivery fare for a number of luggage
pieces.
The fare for a piece is determined by its width
w
, depth
d
and height
h
, each rounded up to an integer.
The fare is proportional to the
sum
of them.
The list of the luggage pieces is at hand,
but the list has 
the
product
w
×
d
×
h
for each piece, instead of the
sum
w
+
d
+
h
by mistake.
This
information is not enough to calculate the exact delivery fare.
Mr. Yokohama therefore decided to estimate the minimum possible
delivery fare.
To this end, given the listed
product
p
for each of the luggage pieces, the minimum possible
sum
s
=
w
+
d
+
h
satisfying
p
=
w
×
d
×
h
should be
found.
You are requested to help Mr. Yokohama by writing a program that, when given
the product
p
, computes the minimum sum
s.
Input
The input consists of multiple datasets.
  Each of the datasets has one line containing an integer
p
(0 <
p
< 10
15
).
The end of the input is indicated by a line containing a zero.
The number of datasets does not exceed 300.
Output
For each dataset, output a single line containing an integer
s
.
s
should be the minimum possible sum
w
+
d
+
h
of three positive integers,
w,
d,
and
h,
satisfying
p
=
w
×
d
×
h.
Sample Input
1
2
6
8
729
47045881
12137858827249
562949953421312
986387345919360
999730024299271
999998765536093
0
Output for the Sample Input
3
4
6
6
27
1083
6967887
262144
298633
299973
999998765536095

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int N=4e7+3;
bool isComposite[N];
long long primes[N], sz;
void sieve() { // save the all primes till N in  linear time
    isComposite[0] = isComposite[1] = true;
    for (int i = 2; i < N; ++i) {
        if (!isComposite[i]) primes[sz++] = i;
        for (int j = 0; j < sz && primes[j] * i < N; ++j) {
            isComposite[primes[j] * i] = true;
            if (i % primes[j] == 0) break;
        }
    }
}
int FirstPrimeIndexBiggerThan_1e5;
vector<long long> HowManyPrimesBiggerThan_1e5(long long n){// given n and return the primes factor bigger than 1e5
    vector<long long>ret;
    for (int i = FirstPrimeIndexBiggerThan_1e5; i <sz&&primes[i]*primes[i]<=n ; ++i) {
        while(n%primes[i]==0)
            ret.push_back(primes[i]),n/=primes[i];
    }
    return ret;
}
void solve(long long n) {
    vector<long long>PrimesBiggerThan_1e5= HowManyPrimesBiggerThan_1e5(n);
    long long best=n+2;//the best save the best answer till now
    // of course "1 1 n" which his summation n+2  is the worst answer
    if(PrimesBiggerThan_1e5.size()==2) {
        best = PrimesBiggerThan_1e5[0] + PrimesBiggerThan_1e5[1] +
               n / (PrimesBiggerThan_1e5[0] * PrimesBiggerThan_1e5[1]);
    }
    else if(PrimesBiggerThan_1e5.size()==1){
        n/=PrimesBiggerThan_1e5[0];
        for (int  i= 1; i<=min(n,(long long)1e5) ; ++i) {
            if(n%i==0)
                best=min(best,PrimesBiggerThan_1e5[0]+i+n/i);
        }
    }
    else{
        vector<long long>factor;
        for (int i = 1; i<=min(n,(long long)1e5) ; ++i) {
            if(n%i==0){
                factor.push_back(i);
                for (int j = 0; j <factor.size()&&i*factor[j]<=n ; ++j) {
                    if(n%(i*factor[j])==0)
                        best=min(best,i+factor[j]+n/(i*factor[j]));
                }
            }
        }
    }
    cout<<best<<'\n';

}
int main() {
    sieve();
    FirstPrimeIndexBiggerThan_1e5= upper_bound(primes,primes+sz,1e5)-primes;
    long long n;
    while (cin>>n,n)
        solve(n);
}
accuracy: 1
time_ms: 4760
memory_kb: 64296
final_rank: 17

code_snippet: #line 1 "2020C.cpp"
#include <bits/stdc++.h>
#line 1 "/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp"


#line 6 "/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp"
using namespace std;
vector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }
vector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }
vector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }
vector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }


#line 3 "2020C.cpp"

using namespace std;

// using namespace atcoder;

#ifdef ADRY
#include <dbg.h>
#else
// DO NOTHING
#define dbg(x)
#endif


#define all(v) v.begin(),v.end()
#define resort(v) sort(v.rbegin(),v.rend())
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll,ll>;
using vp=vector<pair<ll, ll>>;
using djks=priority_queue<P, vp, greater<P>>;

const ll inf=1ll<<60;
#define mod10 (ll)1e9+7
#define mod99 (ll)998244353
const double PI = acos(-1);

#define rep(i,n) for (ll i=0;i<(n);++i)
#define per(i,n) for(ll i=(n)-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=(a);i<(n);++i)
#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)


template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

ll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};
ll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};

// https://algo-method.com/tasks/553/editorial
// Miller-Rabin 素数判定法
template<class T> T pow_mod(T A, T N, T M) {
    T res = 1 % M;
    A %= M;
    while (N) {
        if (N & 1) res = (res * A) % M;
        A = (A * A) % M;
        N >>= 1;
    }
    return res;
}

bool is_prime(long long N) {
    if (N <= 1) return false;
    if (N == 2 || N == 3) return true;
    if (N % 2 == 0) return false;
    vector<long long> A = {2, 325, 9375, 28178, 450775,
                           9780504, 1795265022};
    long long s = 0, d = N - 1;
    while (d % 2 == 0) {
        ++s;
        d >>= 1;
    }
    for (auto a : A) {
        if (a % N == 0) return true;
        long long t, x = pow_mod<__int128_t>(a, d, N);
        if (x != 1) {
            for (t = 0; t < s; ++t) {
                if (x == N - 1) break;
                x = __int128_t(x) * x % N;
            }
            if (t == s) return false;
        }
    }
    return true;
}

// Pollard のロー法
long long gcd(long long A, long long B) {
    A = abs(A), B = abs(B);
    if (B == 0) return A;
    else return gcd(B, A % B);
}
    
long long pollard(long long N) {
    if (N % 2 == 0) return 2;
    if (is_prime(N)) return N;

    long long step = 0;
    auto f = [&](long long x) -> long long {
        return (__int128_t(x) * x + step) % N;
    };
    while (true) {
        ++step;
        long long x = step, y = f(x);
        while (true) {
            long long p = gcd(y - x + N, N);
            if (p == 0 || p == N) break;
            if (p != 1) return p;
            x = f(x);
            y = f(f(y));
        }
    }
}

vector<long long> prime_factorize(long long N) {
    if (N == 1) return {};
    long long p = pollard(N);
    if (p == N) return {p};
    vector<long long> left = prime_factorize(p);
    vector<long long> right = prime_factorize(N / p);
    left.insert(left.end(), right.begin(), right.end());
    sort(left.begin(), left.end());
    return left;
}


int solve(){
    ll p; cin >> p;
    if(p==0) return 1;
    auto a = prime_factorize(p);
    
    auto uniqued_a = a;
    uniqued_a.erase(unique(all(uniqued_a)), uniqued_a.end());

    map<ll,ll> mp;
    for(auto x: a) mp[x]++;
    dbg(a);
    dbg(mp);
    vll divisors;

    auto dfs = [&](auto self, ll cur, int i) {
        if(i==uniqued_a.size()) {
            divisors.emplace_back(cur);
            return;
        }

        ll muler = 1;
        rep(c, mp[uniqued_a[i]]+1) {
            cur *= muler;
            self(self,cur,i+1);
            cur /= muler;
            muler *= uniqued_a[i];
        }
        return;
    };

    dfs(dfs,1,0);
    sort(all(divisors));

    dbg(divisors);

    ll ans = inf;
for (auto a : divisors) {
    if (a * a * a > p) break;
    ll left = p / a;
    ll root = sqrtl((long double)left);
    auto it = upper_bound(divisors.begin(), divisors.end(), root);
    while (it != divisors.begin()) {
        --it;
        ll d = *it;
        if (d < a) break;
        if (left % d == 0) {
            ll h = left / d;
            ans = min(ans, a + d + h);
            break;
        }
    }
}
    cout << ans << '\n';
    return 0;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(!solve());
}
accuracy: 1
time_ms: 80
memory_kb: 3836
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
// エラトステネスの篩
struct Eratosthenes {
  // テーブル
  vector<bool> isprime;

  // 整数 i を割り切る最小の素数
  vector<ll> minfactor;

  vector<ll> mobius;

  // コンストラクタで篩を回す
  Eratosthenes(ll N)
      : isprime(N + 1, true), minfactor(N + 1, -1), mobius(N + 1, 1) {
    // 1 は予めふるい落としておく
    isprime[1] = false;
    minfactor[1] = 1;

    // 篩
    for (ll p = 2; p <= N; ++p) {
      // すでに合成数であるものはスキップする
      if (!isprime[p]) continue;

      // p についての情報更新
      minfactor[p] = p;
      mobius[p] = -1;

      // p 以外の p の倍数から素数ラベルを剥奪
      for (ll q = p * 2; q <= N; q += p) {
        // q は合成数なのでふるい落とす
        isprime[q] = false;

        // q は p で割り切れる旨を更新
        if (minfactor[q] == -1) minfactor[q] = p;
        if ((q / p) % p == 0)
          mobius[q] = 0;
        else
          mobius[q] = -mobius[q];
      }
    }
  }

  // 高速素因数分解
  // pair (素因子, 指数) の vector を返す
  vector<pair<ll, ll>> factorize(ll n) {
    vector<pair<ll, ll>> res;
    while (n > 1) {
      ll p = minfactor[n];
      ll exp = 0;

      // n で割り切れる限り割る
      while (minfactor[n] == p) {
        n /= p;
        ++exp;
      }
      res.emplace_back(p, exp);
    }
    return res;
  }
  vector<ll> divisors(ll n) {
    vector<ll> res({1});
    auto pf = factorize(n);
    for (auto p : pf) {
      ll s = (ll)res.size();
      for (ll i = 0; i < s; i++) {
        ll v = 1;
        for (ll j = 0; j < p.second; j++) {
          v *= p.first;
          res.push_back(res[i] * v);
        }
      }
    }
    return res;
  }
};
int main() {
  vector<char> er(34000000, 1);
  er[0] = 0;
  er[1] = 1;
  vector<ll> primes;
  for (ll i = 2; i < er.size(); i++) {
    if (er[i]) {
      primes.push_back(i);
      for (ll j = i * 2; j < er.size(); j += i) {
        er[j] = 0;
      }
    }
  }
  while (1) {
    ll p;
    cin >> p;
    if (p == 0) break;

    using a2 = array<ll, 2>;
    vector<a2> cnt;
    ll n = p;
    for (auto x : primes) {
      if (x * x > n) break;
      ll c = 0;
      while (n % x == 0) {
        n /= x;
        c++;
      }
      if (c) cnt.push_back({x, c});
    }
    if (n > 1) cnt.push_back({n, 1});

    vector<ll> div;

    auto make = [&](auto self, ll now, ll depth) -> void {
      if (depth == cnt.size()) {
        div.push_back(now);
      } else {
        for (int i = 0; i <= cnt[depth][1]; i++) {
          self(self, now, depth + 1);
          now *= cnt[depth][0];
        }
      }
    };

    make(make, 1, 0);

    sort(div.begin(), div.end());
    unordered_set<ll> sh;
    for (auto x : div) sh.insert(x);

    ll ans = p + 2;
    for (int i = 0; i < div.size(); i++) {
      if (div[i] * div[i] * div[i] > p) break;

      ll n = p / div[i];

      ll sq = sqrt(n);
      auto itr = lower_bound(div.begin(), div.end(), sq);
      while (itr != div.end()) {
        if (n / (*itr) < div[i]) break;
        if (n % (*itr) == 0 && sh.count(n / (*itr))) {
          ans = min(ans, div[i] + (*itr) + n / (*itr));
        }
        itr++;
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 4190
memory_kb: 54336
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

# pragma GCC target("avx2")
# pragma GCC optimize("O3")
# pragma GCC optimize("unroll-loops")

using ll = long long;

int isqrt(ll N) {
    ll ok = 0, ng = 1e8;
    while(ok + 1 < ng) {
        ll x = (ok + ng) / 2;
        if(x * x <= N) ok = x;
        else ng = x;
    }
    return ok;
}

int icbrt(ll N) {
    ll ok = 0, ng = 2e5;
    while(ok + 1 < ng) {
        ll x = (ok + ng) / 2;
        if(x * x * x <= N) ok = x;
        else ng = x;
    }
    return ok;
}

bool solve() {
    ll N;
    cin >> N;
    if(N == 0) return false;
    const int X = icbrt(N);
    ll ans = N + 2;
    for(int x = X, c = 0; x >= 1 && c < 30; --x) {
        if(N % x == 0) {
            const int Y = isqrt(N / x);
            for(ll y = Y; y >= x; --y) {
                if((N / x) % y == 0) {
                    c++;
                    ans = min(ans, x + y + (N / x / y));
                    break;
                }
            }
        }
    }
    cout << ans << endl;
    return true;
}

int main() {
    while(solve());
}
accuracy: 1
time_ms: 7400
memory_kb: 3456
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#line 2 "prime/fast-factorize.hpp"

#include <cstdint>
#include <numeric>
#include <vector>
using namespace std;

#line 2 "internal/internal-math.hpp"

#line 2 "internal/internal-type-traits.hpp"

#include <type_traits>
using namespace std;

namespace internal {
template <typename T>
using is_broadly_integral =
    typename conditional_t<is_integral_v<T> || is_same_v<T, __int128_t> ||
                               is_same_v<T, __uint128_t>,
                           true_type, false_type>::type;

template <typename T>
using is_broadly_signed =
    typename conditional_t<is_signed_v<T> || is_same_v<T, __int128_t>,
                           true_type, false_type>::type;

template <typename T>
using is_broadly_unsigned =
    typename conditional_t<is_unsigned_v<T> || is_same_v<T, __uint128_t>,
                           true_type, false_type>::type;

#define ENABLE_VALUE(x) \
  template <typename T> \
  constexpr bool x##_v = x<T>::value;

ENABLE_VALUE(is_broadly_integral);
ENABLE_VALUE(is_broadly_signed);
ENABLE_VALUE(is_broadly_unsigned);
#undef ENABLE_VALUE

#define ENABLE_HAS_TYPE(var)                                   \
  template <class, class = void>                               \
  struct has_##var : false_type {};                            \
  template <class T>                                           \
  struct has_##var<T, void_t<typename T::var>> : true_type {}; \
  template <class T>                                           \
  constexpr auto has_##var##_v = has_##var<T>::value;

#define ENABLE_HAS_VAR(var)                                     \
  template <class, class = void>                                \
  struct has_##var : false_type {};                             \
  template <class T>                                            \
  struct has_##var<T, void_t<decltype(T::var)>> : true_type {}; \
  template <class T>                                            \
  constexpr auto has_##var##_v = has_##var<T>::value;

}  // namespace internal
#line 4 "internal/internal-math.hpp"

namespace internal {

#include <cassert>
#include <utility>
#line 10 "internal/internal-math.hpp"
using namespace std;

// a mod p
template <typename T>
T safe_mod(T a, T p) {
  a %= p;
  if constexpr (is_broadly_signed_v<T>) {
    if (a < 0) a += p;
  }
  return a;
}

// 返り値：pair(g, x)
// s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
template <typename T>
pair<T, T> inv_gcd(T a, T p) {
  static_assert(is_broadly_signed_v<T>);
  a = safe_mod(a, p);
  if (a == 0) return {p, 0};
  T b = p, x = 1, y = 0;
  while (a != 0) {
    T q = b / a;
    swap(a, b %= a);
    swap(x, y -= q * x);
  }
  if (y < 0) y += p / b;
  return {b, y};
}

// 返り値 : a^{-1} mod p
// gcd(a, p) != 1 が必要
template <typename T>
T inv(T a, T p) {
  static_assert(is_broadly_signed_v<T>);
  a = safe_mod(a, p);
  T b = p, x = 1, y = 0;
  while (a != 0) {
    T q = b / a;
    swap(a, b %= a);
    swap(x, y -= q * x);
  }
  assert(b == 1);
  return y < 0 ? y + p : y;
}

// T : 底の型
// U : T*T がオーバーフローしない かつ 指数の型
template <typename T, typename U>
T modpow(T a, U n, T p) {
  a = safe_mod(a, p);
  T ret = 1 % p;
  while (n != 0) {
    if (n % 2 == 1) ret = U(ret) * a % p;
    a = U(a) * a % p;
    n /= 2;
  }
  return ret;
}

// 返り値 : pair(rem, mod)
// 解なしのときは {0, 0} を返す
template <typename T>
pair<T, T> crt(const vector<T>& r, const vector<T>& m) {
  static_assert(is_broadly_signed_v<T>);
  assert(r.size() == m.size());
  int n = int(r.size());
  T r0 = 0, m0 = 1;
  for (int i = 0; i < n; i++) {
    assert(1 <= m[i]);
    T r1 = safe_mod(r[i], m[i]), m1 = m[i];
    if (m0 < m1) swap(r0, r1), swap(m0, m1);
    if (m0 % m1 == 0) {
      if (r0 % m1 != r1) return {0, 0};
      continue;
    }
    auto [g, im] = inv_gcd(m0, m1);
    T u1 = m1 / g;
    if ((r1 - r0) % g) return {0, 0};
    T x = (r1 - r0) / g % u1 * im % u1;
    r0 += x * m0;
    m0 *= u1;
    if (r0 < 0) r0 += m0;
  }
  return {r0, m0};
}

}  // namespace internal
#line 2 "misc/rng.hpp"

#line 2 "internal/internal-seed.hpp"

#include <chrono>
using namespace std;

namespace internal {
unsigned long long non_deterministic_seed() {
  unsigned long long m =
      chrono::duration_cast<chrono::nanoseconds>(
          chrono::high_resolution_clock::now().time_since_epoch())
          .count();
  m ^= 9845834732710364265uLL;
  m ^= m << 24, m ^= m >> 31, m ^= m << 35;
  return m;
}
unsigned long long deterministic_seed() { return 88172645463325252UL; }

// 64 bit の seed 値を生成 (手元では seed 固定)
// 連続で呼び出すと同じ値が何度も返ってくるので注意
// #define RANDOMIZED_SEED するとシードがランダムになる
unsigned long long seed() {
#if defined(NyaanLocal) && !defined(RANDOMIZED_SEED)
  return deterministic_seed();
#else
  return non_deterministic_seed();
#endif
}

}  // namespace internal
#line 4 "misc/rng.hpp"

namespace my_rand {
using i64 = long long;
using u64 = unsigned long long;

// [0, 2^64 - 1)
u64 rng() {
  static u64 _x = internal::seed();
  return _x ^= _x << 7, _x ^= _x >> 9;
}

// [l, r]
i64 rng(i64 l, i64 r) {
  assert(l <= r);
  return l + rng() % u64(r - l + 1);
}

// [l, r)
i64 randint(i64 l, i64 r) {
  assert(l < r);
  return l + rng() % u64(r - l);
}

// choose n numbers from [l, r) without overlapping
vector<i64> randset(i64 l, i64 r, i64 n) {
  assert(l <= r && n <= r - l);
  unordered_set<i64> s;
  for (i64 i = n; i; --i) {
    i64 m = randint(l, r + 1 - i);
    if (s.find(m) != s.end()) m = r - i;
    s.insert(m);
  }
  vector<i64> ret;
  for (auto& x : s) ret.push_back(x);
  sort(begin(ret), end(ret));
  return ret;
}

// [0.0, 1.0)
double rnd() { return rng() * 5.42101086242752217004e-20; }
// [l, r)
double rnd(double l, double r) {
  assert(l < r);
  return l + rnd() * (r - l);
}

template <typename T>
void randshf(vector<T>& v) {
  int n = v.size();
  for (int i = 1; i < n; i++) swap(v[i], v[randint(0, i + 1)]);
}

}  // namespace my_rand

using my_rand::randint;
using my_rand::randset;
using my_rand::randshf;
using my_rand::rnd;
using my_rand::rng;
#line 2 "modint/arbitrary-montgomery-modint.hpp"

#include <iostream>
using namespace std;

template <typename Int, typename UInt, typename Long, typename ULong, int id>
struct ArbitraryLazyMontgomeryModIntBase {
  using mint = ArbitraryLazyMontgomeryModIntBase;

  inline static UInt mod;
  inline static UInt r;
  inline static UInt n2;
  static constexpr int bit_length = sizeof(UInt) * 8;

  static UInt get_r() {
    UInt ret = mod;
    while (mod * ret != 1) ret *= UInt(2) - mod * ret;
    return ret;
  }
  static void set_mod(UInt m) {
    assert(m < (UInt(1u) << (bit_length - 2)));
    assert((m & 1) == 1);
    mod = m, n2 = -ULong(m) % m, r = get_r();
  }
  UInt a;

  ArbitraryLazyMontgomeryModIntBase() : a(0) {}
  ArbitraryLazyMontgomeryModIntBase(const Long &b)
      : a(reduce(ULong(b % mod + mod) * n2)){};

  static UInt reduce(const ULong &b) {
    return (b + ULong(UInt(b) * UInt(-r)) * mod) >> bit_length;
  }

  mint &operator+=(const mint &b) {
    if (Int(a += b.a - 2 * mod) < 0) a += 2 * mod;
    return *this;
  }
  mint &operator-=(const mint &b) {
    if (Int(a -= b.a) < 0) a += 2 * mod;
    return *this;
  }
  mint &operator*=(const mint &b) {
    a = reduce(ULong(a) * b.a);
    return *this;
  }
  mint &operator/=(const mint &b) {
    *this *= b.inverse();
    return *this;
  }

  mint operator+(const mint &b) const { return mint(*this) += b; }
  mint operator-(const mint &b) const { return mint(*this) -= b; }
  mint operator*(const mint &b) const { return mint(*this) *= b; }
  mint operator/(const mint &b) const { return mint(*this) /= b; }

  bool operator==(const mint &b) const {
    return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);
  }
  bool operator!=(const mint &b) const {
    return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);
  }
  mint operator-() const { return mint(0) - mint(*this); }
  mint operator+() const { return mint(*this); }

  mint pow(ULong n) const {
    mint ret(1), mul(*this);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul, n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const mint &b) {
    return os << b.get();
  }

  friend istream &operator>>(istream &is, mint &b) {
    Long t;
    is >> t;
    b = ArbitraryLazyMontgomeryModIntBase(t);
    return (is);
  }

  mint inverse() const {
    Int x = get(), y = get_mod(), u = 1, v = 0;
    while (y > 0) {
      Int t = x / y;
      swap(x -= t * y, y);
      swap(u -= t * v, v);
    }
    return mint{u};
  }

  UInt get() const {
    UInt ret = reduce(a);
    return ret >= mod ? ret - mod : ret;
  }

  static UInt get_mod() { return mod; }
};

// id に適当な乱数を割り当てて使う
template <int id>
using ArbitraryLazyMontgomeryModInt =
    ArbitraryLazyMontgomeryModIntBase<int, unsigned int, long long,
                                      unsigned long long, id>;
template <int id>
using ArbitraryLazyMontgomeryModInt64bit =
    ArbitraryLazyMontgomeryModIntBase<long long, unsigned long long, __int128_t,
                                      __uint128_t, id>;
#line 2 "prime/miller-rabin.hpp"

#line 4 "prime/miller-rabin.hpp"
using namespace std;

#line 8 "prime/miller-rabin.hpp"

namespace fast_factorize {

template <typename T, typename U>
bool miller_rabin(const T& n, vector<T> ws) {
  if (n <= 2) return n == 2;
  if (n % 2 == 0) return false;

  T d = n - 1;
  while (d % 2 == 0) d /= 2;
  U e = 1, rev = n - 1;
  for (T w : ws) {
    if (w % n == 0) continue;
    T t = d;
    U y = internal::modpow<T, U>(w, t, n);
    while (t != n - 1 && y != e && y != rev) y = y * y % n, t *= 2;
    if (y != rev && t % 2 == 0) return false;
  }
  return true;
}

bool miller_rabin_u64(unsigned long long n) {
  return miller_rabin<unsigned long long, __uint128_t>(
      n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
}

template <typename mint>
bool miller_rabin(unsigned long long n, vector<unsigned long long> ws) {
  if (n <= 2) return n == 2;
  if (n % 2 == 0) return false;

  if (mint::get_mod() != n) mint::set_mod(n);
  unsigned long long d = n - 1;
  while (~d & 1) d >>= 1;
  mint e = 1, rev = n - 1;
  for (unsigned long long w : ws) {
    if (w % n == 0) continue;
    unsigned long long t = d;
    mint y = mint(w).pow(t);
    while (t != n - 1 && y != e && y != rev) y *= y, t *= 2;
    if (y != rev && t % 2 == 0) return false;
  }
  return true;
}

bool is_prime(unsigned long long n) {
  using mint32 = ArbitraryLazyMontgomeryModInt<96229631>;
  using mint64 = ArbitraryLazyMontgomeryModInt64bit<622196072>;

  if (n <= 2) return n == 2;
  if (n % 2 == 0) return false;
  if (n < (1uLL << 30)) {
    return miller_rabin<mint32>(n, {2, 7, 61});
  } else if (n < (1uLL << 62)) {
    return miller_rabin<mint64>(
        n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
  } else {
    return miller_rabin_u64(n);
  }
}

}  // namespace fast_factorize

using fast_factorize::is_prime;

/**
 * @brief Miller-Rabin primality test
 */
#line 12 "prime/fast-factorize.hpp"

namespace fast_factorize {
using u64 = uint64_t;

template <typename mint, typename T>
T pollard_rho(T n) {
  if (~n & 1) return 2;
  if (is_prime(n)) return n;
  if (mint::get_mod() != n) mint::set_mod(n);
  mint R, one = 1;
  auto f = [&](mint x) { return x * x + R; };
  auto rnd_ = [&]() { return rng() % (n - 2) + 2; };
  while (1) {
    mint x, y, ys, q = one;
    R = rnd_(), y = rnd_();
    T g = 1;
    constexpr int m = 128;
    for (int r = 1; g == 1; r <<= 1) {
      x = y;
      for (int i = 0; i < r; ++i) y = f(y);
      for (int k = 0; g == 1 && k < r; k += m) {
        ys = y;
        for (int i = 0; i < m && i < r - k; ++i) q *= x - (y = f(y));
        g = gcd(q.get(), n);
      }
    }
    if (g == n) do
        g = gcd((x - (ys = f(ys))).get(), n);
      while (g == 1);
    if (g != n) return g;
  }
  exit(1);
}

using i64 = long long;

vector<i64> inner_factorize(u64 n) {
  using mint32 = ArbitraryLazyMontgomeryModInt<452288976>;
  using mint64 = ArbitraryLazyMontgomeryModInt64bit<401243123>;

  if (n <= 1) return {};
  u64 p;
  if (n <= (1LL << 30)) {
    p = pollard_rho<mint32, uint32_t>(n);
  } else if (n <= (1LL << 62)) {
    p = pollard_rho<mint64, uint64_t>(n);
  } else {
    exit(1);
  }
  if (p == n) return {i64(p)};
  auto l = inner_factorize(p);
  auto r = inner_factorize(n / p);
  copy(begin(r), end(r), back_inserter(l));
  return l;
}

vector<i64> factorize(u64 n) {
  auto ret = inner_factorize(n);
  sort(begin(ret), end(ret));
  return ret;
}

map<i64, i64> factor_count(u64 n) {
  map<i64, i64> mp;
  for (auto &x : factorize(n)) mp[x]++;
  return mp;
}

vector<i64> divisors(u64 n) {
  if (n == 0) return {};
  vector<pair<i64, i64>> v;
  for (auto &p : factorize(n)) {
    if (v.empty() || v.back().first != p) {
      v.emplace_back(p, 1);
    } else {
      v.back().second++;
    }
  }
  vector<i64> ret;
  auto f = [&](auto rc, int i, i64 x) -> void {
    if (i == (int)v.size()) {
      ret.push_back(x);
      return;
    }
    rc(rc, i + 1, x);
    for (int j = 0; j < v[i].second; j++) rc(rc, i + 1, x *= v[i].first);
  };
  f(f, 0, 1);
  sort(begin(ret), end(ret));
  return ret;
}

}  // namespace fast_factorize

using fast_factorize::divisors;
using fast_factorize::factor_count;
using fast_factorize::factorize;

/**
 * @brief 高速素因数分解(Miller Rabin/Pollard's Rho)
 * @docs docs/prime/fast-factorize.md
 */
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define loop(n) for (int tjh = 0; tjh < (int)(n); tjh++)
constexpr ll ma=10'000'000'000'000'000LL;
ll N;ll res;
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cout<<fixed<<setprecision(16);
  for (;cin>>N,N;) {
    vector<ll>d=factorize(N);
    vector<pair<ll,int>>d0;
    for (const ll i:d) {
      if (d0.empty()||d0.back().first!=i)d0.emplace_back(i,1);
      else ++d0.back().second;
    }
    vector<vector<ll>>d1(d0.size());
    rep(i,d0.size()) {
      d1[i].resize(d0[i].second+1);
      d1[i][0]=1;
      rep(j,d0[i].second)d1[i][j+1]=d1[i][j]*d0[i].first;
    }
    res=ma;
    {
      auto f=[&](auto self,const int p,const ll l,const ll r)->void {
        if (p==(int)d0.size()) {
          if (l+r+N/l/r<res)res=l+r+N/l/r;
          return;
        }
        rep(i,d0[p].second+1)rep(j,d0[p].second-i+1) {
          self(self,p+1,l*d1[p][i],r*d1[p][j]);
        }
        return;
      };
      f(f,0,(ll)1,(ll)1);
    }
    cout<<res<<'\n';
  }
}
accuracy: 1
time_ms: 3640
memory_kb: 3584
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct montgomery_modint {
    using int64 = uint64_t;
    using int128 = __uint128_t;
    using modint = montgomery_modint;
    montgomery_modint() : x(0) {}
    montgomery_modint(long long v) : x(reduce((int128(v) + MOD) * R)) {}
    static void set_mod(long long _m) {
        MOD = _m;
        R = -int128(MOD) % MOD;
        INV = get_inv_mod(); 
    }
    static long long mod() { return MOD; }
    long long val() const {
        int64 res = reduce(x);
        return res >= MOD ? res - MOD : res;
    }
    modint& operator+=(const modint &r) {
        x += r.x;
        if (x >= (MOD << 1)) x -= (MOD << 1);
        return *this;
    }
    modint& operator-=(const modint &r) {
        x += (MOD << 1) - r.x;
        if (x >= (MOD << 1)) x -= (MOD << 1);
        return *this;
    }
    modint& operator*=(const modint &r) {
        x = reduce(int128(x) * r.x);
        return *this;
    }
    modint& operator/=(const modint &r) {
        *this *= r.inv();
        return *this;
    }
    friend modint operator+(const modint &a, const modint &b) {
        return modint(a) += b;
    }
    friend modint operator-(const modint &a, const modint &b) {
        return modint(a) -= b;
    }
    friend modint operator*(const modint &a, const modint &b) {
        return modint(a) *= b;
    }
    friend modint operator/(const modint &a, const modint &b) {
        return modint(a) /= b;
    }
    friend bool operator==(const modint &a, const modint &b) {
        return a.val() == b.val();
    }
    friend bool operator!=(const modint &a, const modint &b) {
        return a.val() != b.val();
    }
    modint operator+() const { return *this; }
    modint operator-() const { return modint() - *this; }
    modint inv() const { return pow(MOD - 2); }
    modint pow(int128 k) const {
        modint a = *this;
        modint res = 1;
        while (k > 0) {
            if (k & 1) res *= a;
            a *= a;
            k >>= 1;
        }
        return res;
    }
private:
    int64 x;
    static int64 MOD, INV, R;
    static int64 get_inv_mod() {
        int64 res = MOD;
        for (int t = 0; t < 5; t++) res *= 2 - MOD * res;
        return res;
    }
    static int64 reduce(const int128 &v) {
        return (v + int128(int64(v) * int64(-INV)) * MOD) >> 64;
    }
};
typename montgomery_modint::int64
montgomery_modint::MOD, montgomery_modint::INV, montgomery_modint::R;

bool miller_rabin(long long m, const vector<long long> ps) {
    using mint = montgomery_modint;
    mint::set_mod(m);
    long long u = 0, v = m - 1;
    while ((v & 1) == 0) u++, v >>= 1;
    for (long long p : ps) {
        if (m <= p) return true;
        mint x = mint(p).pow(v);
        if (x != 1) {
            long long w;
            for (w = 0; w < u; w++) {
                if (x == m - 1) break;
                x *= x;
            }
            if (u == w) return false;
        }
    }
    return true;
}

bool miller_rabin_small(long long m) {
    return miller_rabin(m, {2, 7, 61});
}

bool miller_rabin_large(long long m) {
    return miller_rabin(m, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
}

bool is_prime(long long m) {
    if (m <= 1) return false;
    if (m == 2) return true;
    if (m % 2 == 0) return false;
    return m < 4759123141LL ? miller_rabin_small(m) : miller_rabin_large(m);
}

template<typename T>
T rho(T n) {
    for (int p : {2, 3, 5, 7}) {
        if (n % p == 0) return p;
    }
    auto randint_64 = [&](T low, T hi) -> T {
        random_device seed;
        mt19937_64 rng(seed());
        uniform_int_distribution<T> dist(low, hi);
        return dist(rng);
    };
    using mint = montgomery_modint;
    mint::set_mod(n);
    while (true) {
        mint u = randint_64(2, n - 1);
        mint v = u;
        mint c = randint_64(1, n - 1);
        T d = 1;
        while (d == 1) {
            u = u * u + c;
            v = v * v + c;
            v = v * v + c;
            d = gcd((u - v).val(), n);
        }
        if (d < n) return d;
    }
    return -1;
}

template<typename T>
vector<T> prime_factor(T n) {
    if (n <= 1) return {};
    if (is_prime(n)) return {n};
    vector<T> res;
    T d = rho(n);
    auto a = prime_factor(d);
    auto b = prime_factor(n / d);
    merge(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res));
    res.erase(unique(res.begin(), res.end()), res.end());
    return res;
}

vector<pair<long long, int>> factorise(long long n) {
    auto ps = prime_factor(n);
    vector<pair<long long, int>> res;
    res.reserve(ps.size());
    for (auto p : ps) {
        res.emplace_back(p, 0);
        while (n % p == 0) {
            res.back().second++;
            n /= p;
        }
    }
    return res;
}

template<typename T>
vector<T> enumerate_divisors(T n) {
    auto f = factorise(n);
    int m = f.size();
    vector<T> res;
    auto rec = [&](const auto &rec, int i, T x) -> void {
        if (i == m) {
            res.push_back(x);
            return;
        }
        auto [p, k] = f[i];
        for (int j = 0; j <= k; j++) {
            rec(rec, i + 1, x);
            x *= p;
        }
    };
    rec(rec, 0, 1);
    sort(res.begin(), res.end());
    return res;
}

bool solve() {
    long long P;
    cin >> P;
    if (P == 0) return false;
    auto D = enumerate_divisors(P);
    int N = D.size();
    long long ans = 1LL << 60;
    for (int i = 0; i < N && D[i] * D[i] * D[i] <= P; i++) {
        for (int j = i; j < N && D[i] * D[j] * D[j] <= P; j++) {
            if (P % (D[i] * D[j]) == 0) {
                ans = min(ans, D[i] + D[j] + P / (D[i] * D[j]));
            }
        }
    }
    cout << ans << '\n';
    return true;
}

int main() {
    while (solve()) {}
}
accuracy: 1
time_ms: 1680
memory_kb: 3804
final_rank: 3

code_snippet: // #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;

using ll = long long;
using ull = unsigned long long;
using vecint = std::vector<int>;
using vecll = std::vector<long long>;
using vecstr = std::vector<string>;
using vecbool = std::vector<bool>;
using vecdou = std::vector<double>;
using vecpl = std::vector<pair<ll,ll>>;
using vec2d = std::vector<vecll>;
using vec2di = std::vector<vecint>;
using vec2dd = std::vector<vecdou>;
using vec2db = std::vector<vecbool>;
using pl = pair<long long,long long>;
using mint998 = modint998244353;
using mint107 = modint1000000007;
using mint = modint;
using vecmint = std::vector<mint>;
using vecmint998 = std::vector<mint998>;
using vecmint107 = std::vector<mint107>;
using vec2dmint = std::vector<vecmint>; 
using vec2dmint998 = std::vector<vecmint998>;
using vec2dmint107 = std::vector<vecmint107>;
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i,n) for (ll i = 1; i <= (ll)(n); i++)
#define REP(i,l,r) for (ll i = (ll)(l); i < (ll)(r); i++)
#define rrep(i,n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define rrep1(i,n) for (ll i = (ll)(n); i > 0; i--)
#define RREP(i,l,r) for (ll i = (ll)(r)-1; i >= (ll)(l); i--)
#define all(a) (a).begin(), (a).end()
#define INF ((1LL<<62)-(1LL<<31))
#define inr(a,x,b) ((a) <= (x) && (x) < (b))
template <typename T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

void ynout(bool x,string Tru="Yes",string Wro="No"){
    if(x){
        cout << Tru << '\n';
    }else{
        cout << Wro << '\n';
    }
}
ll power(ll a,ll b,ll mod=INF){
    long long x=1,y=a%mod;
    while(b>0){
        if(b&1ll){
            x=(x*y)%mod;
        }
        y=(y*y)%mod;
        b>>=1;
    }
  return x%mod;
}
ll Pdist2(pair<ll,ll> a,pair<ll,ll> b){
    return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second);
}
double Pdist(pair<ll,ll> a,pair<ll,ll> b){
    return sqrt(Pdist2(a,b));
}
ll PdistM(pair<ll,ll> a,pair<ll,ll> b){
    return abs(a.first-b.first)+abs(a.second-b.second);
}
ll gcd(ll a,ll b){
    if(b==0){
        return a;
    }else{
        return gcd(b,a%b);
    }
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
vector<ll> prime;
bool seen[40000009];
bool solve(){
    ll p;
    cin>>p;
    if(p == 0)return 1;
    vector<ll> soinsuu;
    for(auto v : prime){
        if(p % v == 0)soinsuu.emplace_back(v);
    }
    vector<ll> insuu = {1};
    ll P = p;
    for(auto v : soinsuu){
        vector<ll> t = {1};
        ll c = 1;
        while(P % v == 0){
            P /= v;
            c *= v;
            t.emplace_back(c);
        }
        vector<ll> r;
        for(auto x : insuu){
            for(auto y : t){
                r.emplace_back(x*y);
            }
        }
        insuu = r;
    }
    insuu.emplace_back(2e9);
    sort(all(insuu));
    ll ans = 2e18;
    for(int i=1; i<=1e5; i++){
        if(p % i != 0)continue;
        ll p1 = p / i;
        ll l = 0;
        ll r = insuu.size()-1;
        while(r-l > 1){
            ll m = (l+r)/2;
            if(p1 < insuu[m]*insuu[m]){
                r = m;
            }
            else l = m;
        }
        while(p1 % insuu[l] != 0)l--;
        if(l >= 0)chmin(ans,i+insuu[l]+p1/insuu[l]);
    }
    cout << ans << endl;
    return 0;
}
int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    
    for(int i=2; i<=4e7; i++){
        if(seen[i])continue;
        prime.emplace_back(i);
        for(int j=2; i*j<=4e7; j++){
            seen[i*j] = 1;
        }
    }
    // cout << prime.size() << endl;
    while(1){
        if(solve())return 0;
    }
}
accuracy: 1
time_ms: 2200
memory_kb: 76304
final_rank: 14

code_snippet: // https://codeforces.com/blog/entry/96344
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
 
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
 
#if __cplusplus >= 202002L
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
#endif
 
template<typename T>
bool chmax(T& a, const T& b) {
    bool res = a < b;
    a = max(a, b);
    return res;
}
template<typename T>
bool chmin(T& a, const T& b){
    bool res = a > b;
    a = min(a, b);
    return res;
}
 
typedef vector<long long> vl;
typedef pair<ll,ll> pll;
typedef vector<pair<ll, ll>> vll;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef pair<int,int> pii;
 
const int inf = 1000000009;
const ll linf = 4000000000000000009;
 
 
// https://trap.jp/post/1224/
template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}
 
void print(){
    cout << '\n';
}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';
}
 
template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
 
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
 
template<typename T, typename U>
T SUM(const vector<U> &A){
    T ret = 0;
    for (auto &&a: A) ret += a;
    return ret;
}
 
#define rep1(a)          for(int i = 0; i < a; i++)
#define rep2(i, a)       for(int i = 0; i < a; i++)
#define rep3(i, a, b)    for(int i = a; i < b; i++)
#define rep4(i, a, b, c) for(int i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
 
#define rrep(i, a, b)    for(int i = a; i >= b; i--)
 
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define all(v) v.begin(), v.end()
//---------------------------------
 

vi primes, sPrime(40000000);
ll p;

vll factorize(ll n){
    vll ret;
    for(ll i = 0;  i < primes.size() && primes[i] * primes[i] <= n; i++){
        int cnt = 0;
        while(n % primes[i] == 0){
            cnt++;
            n /= primes[i];
        }
        if(cnt > 0) ret.push_back(mp(primes[i], cnt));
    }
    return ret;
}

void divs(ll n, ll j, vll &pr, vl &d){
    if(n > (ll)sqrt(p) + 10) return;
    if(j == pr.size()){
        d.push_back(n);
        return;
    }
    for(int i = 0; i <= pr[j].se; i++){
        divs(n, j+1, pr, d);
        n *= pr[j].fi;
    }
}

void solve(){
    vll pr = factorize(p);
    vector<ll> di;
    divs(1, 0, pr, di);
    ll ans = p + 2;
    //cout << ans << endl;
    //for(auto v: di) cout << v << endl;
    rep(i, di.size()) rep(j, i, di.size()){
        ll s = di[i] + di[j];
        ll pro = di[i] * di[j];
        if(p % pro == 0){
            s += p / pro;
            chmin(ans, s);
        }
    }

    cout << ans << endl;

}
 
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
 
    for (int i = 2; (ll)i * i <= 100000000000000LL; ++i) {
        if (!sPrime[i]) {
            sPrime[i] = i;
            primes.push_back(i);
        }
        for (int j = 0; j < primes.size() && primes[j] <= sPrime[j]; ++j) {
            if ((ll)i * primes[j] > 40000000) break;
            sPrime[primes[j] * i] = primes[j];
        }
    }

    //cout << "BUUB" << endl;
    cin >> p;
    while(p > 0){
        solve();
        cin >> p;
    }
}
accuracy: 1
time_ms: 5250
memory_kb: 226868
final_rank: 20

code_snippet: #include <bits/extc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define ll long long
#define P pair<ll, ll>
#define ALL(s) (s).begin(), (s).end()
bool IsPrime(ll n) {
  if (n <= 1) return false;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) return false;
  }
  return true;
}
vector<bool> ok(40000001, true);
vector<ll> res;
void era(ll n) {
  ok[0] = false;
  ok[1] = false;
  for (ll i = 2; i <= sqrt(n); ++i) {
    if (IsPrime(i)) {
      for (ll j = i * i; j <= n; j += i) ok[j] = false;
    }
  }
  ///*
  FOR(i, 2, n + 1) if (ok[i]) res.push_back(i);

  // return ok;
}

void solve(ll i, ll a, vector<P> &r, vector<ll> &t) {
  if (i >= (ll)r.size()) {
    t.push_back(a);
    return;
  }
  ll now = 1;
  REP(kk, r[i].second + 1) {
    solve(i + 1, a * now, r, t);
    now *= r[i].first;
  }
}

int main() {
  era(40000000);
  while (1) {
    ll n;
    cin >> n;
    if (n == 0) return 0;
    vector<P> r;
    ll nn = n;
    for (auto &x : res) {
      ll now = 0;
      if (x * x > n) break;
      while (nn % x == 0) {
        nn /= x;
        now++;
      }
      if (now == 0) continue;
      r.push_back(P(x, now));
    }
    if (nn > 1) {
      r.push_back(P(nn, 1));
    }
    sort(ALL(r));
    vector<ll> t;
    solve(0, 1, r, t);
    sort(ALL(t));
    ll t1 = 0, t2 = 0;
    ll sq = sqrt(n);
    REP(i, (ll)t.size()) {
      if (t[i] <= sq) {
        t2 = i;
        if (t[i] * t[i] <= n / t[i]) {
          t1 = i;
        }
      }
    }
    ll ans = 1ll << 60;
    REPR(i, t2) {
      REPR(j, t1) {
        if (n % (t[i] * t[j]) == 0) {
          ans = min(ans, t[i] + t[j] + n / (t[i] * t[j]));
        }
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 6320
memory_kb: 42892
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define TT template<typename T>
TT using vec = vector<T>;
template<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }
template<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }

struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;

// https://nyaannyaan.github.io/library/prime/fast-factorize.hpp

#line 2 "prime/fast-factorize.hpp"

#include <cstdint>
#include <numeric>
#include <vector>
using namespace std;

#line 2 "internal/internal-math.hpp"

#line 2 "internal/internal-type-traits.hpp"

#include <type_traits>
using namespace std;

namespace internal {
template <typename T>
using is_broadly_integral =
    typename conditional_t<is_integral_v<T> || is_same_v<T, __int128_t> ||
                               is_same_v<T, __uint128_t>,
                           true_type, false_type>::type;

template <typename T>
using is_broadly_signed =
    typename conditional_t<is_signed_v<T> || is_same_v<T, __int128_t>,
                           true_type, false_type>::type;

template <typename T>
using is_broadly_unsigned =
    typename conditional_t<is_unsigned_v<T> || is_same_v<T, __uint128_t>,
                           true_type, false_type>::type;

#define ENABLE_VALUE(x) \
  template <typename T> \
  constexpr bool x##_v = x<T>::value;

ENABLE_VALUE(is_broadly_integral);
ENABLE_VALUE(is_broadly_signed);
ENABLE_VALUE(is_broadly_unsigned);
#undef ENABLE_VALUE

#define ENABLE_HAS_TYPE(var)                                   \
  template <class, class = void>                               \
  struct has_##var : false_type {};                            \
  template <class T>                                           \
  struct has_##var<T, void_t<typename T::var>> : true_type {}; \
  template <class T>                                           \
  constexpr auto has_##var##_v = has_##var<T>::value;

#define ENABLE_HAS_VAR(var)                                     \
  template <class, class = void>                                \
  struct has_##var : false_type {};                             \
  template <class T>                                            \
  struct has_##var<T, void_t<decltype(T::var)>> : true_type {}; \
  template <class T>                                            \
  constexpr auto has_##var##_v = has_##var<T>::value;

}  // namespace internal
#line 4 "internal/internal-math.hpp"

namespace internal {

#include <cassert>
#include <utility>
#line 10 "internal/internal-math.hpp"
using namespace std;

// a mod p
template <typename T>
T safe_mod(T a, T p) {
  a %= p;
  if constexpr (is_broadly_signed_v<T>) {
    if (a < 0) a += p;
  }
  return a;
}

// 返り値：pair(g, x)
// s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
template <typename T>
pair<T, T> inv_gcd(T a, T p) {
  static_assert(is_broadly_signed_v<T>);
  a = safe_mod(a, p);
  if (a == 0) return {p, 0};
  T b = p, x = 1, y = 0;
  while (a != 0) {
    T q = b / a;
    swap(a, b %= a);
    swap(x, y -= q * x);
  }
  if (y < 0) y += p / b;
  return {b, y};
}

// 返り値 : a^{-1} mod p
// gcd(a, p) != 1 が必要
template <typename T>
T inv(T a, T p) {
  static_assert(is_broadly_signed_v<T>);
  a = safe_mod(a, p);
  T b = p, x = 1, y = 0;
  while (a != 0) {
    T q = b / a;
    swap(a, b %= a);
    swap(x, y -= q * x);
  }
  assert(b == 1);
  return y < 0 ? y + p : y;
}

// T : 底の型
// U : T*T がオーバーフローしない かつ 指数の型
template <typename T, typename U>
T modpow(T a, U n, T p) {
  a = safe_mod(a, p);
  T ret = 1 % p;
  while (n != 0) {
    if (n % 2 == 1) ret = U(ret) * a % p;
    a = U(a) * a % p;
    n /= 2;
  }
  return ret;
}

// 返り値 : pair(rem, mod)
// 解なしのときは {0, 0} を返す
template <typename T>
pair<T, T> crt(const vector<T>& r, const vector<T>& m) {
  static_assert(is_broadly_signed_v<T>);
  assert(r.size() == m.size());
  int n = int(r.size());
  T r0 = 0, m0 = 1;
  for (int i = 0; i < n; i++) {
    assert(1 <= m[i]);
    T r1 = safe_mod(r[i], m[i]), m1 = m[i];
    if (m0 < m1) swap(r0, r1), swap(m0, m1);
    if (m0 % m1 == 0) {
      if (r0 % m1 != r1) return {0, 0};
      continue;
    }
    auto [g, im] = inv_gcd(m0, m1);
    T u1 = m1 / g;
    if ((r1 - r0) % g) return {0, 0};
    T x = (r1 - r0) / g % u1 * im % u1;
    r0 += x * m0;
    m0 *= u1;
    if (r0 < 0) r0 += m0;
  }
  return {r0, m0};
}

}  // namespace internal
#line 2 "misc/rng.hpp"

#line 2 "internal/internal-seed.hpp"

#include <chrono>
using namespace std;

namespace internal {
unsigned long long non_deterministic_seed() {
  unsigned long long m =
      chrono::duration_cast<chrono::nanoseconds>(
          chrono::high_resolution_clock::now().time_since_epoch())
          .count();
  m ^= 9845834732710364265uLL;
  m ^= m << 24, m ^= m >> 31, m ^= m << 35;
  return m;
}
unsigned long long deterministic_seed() { return 88172645463325252UL; }

// 64 bit の seed 値を生成 (手元では seed 固定)
// 連続で呼び出すと同じ値が何度も返ってくるので注意
// #define RANDOMIZED_SEED するとシードがランダムになる
unsigned long long seed() {
#if defined(NyaanLocal) && !defined(RANDOMIZED_SEED)
  return deterministic_seed();
#else
  return non_deterministic_seed();
#endif
}

}  // namespace internal
#line 4 "misc/rng.hpp"

namespace my_rand {
using i64 = long long;
using u64 = unsigned long long;

// [0, 2^64 - 1)
u64 rng() {
  static u64 _x = internal::seed();
  return _x ^= _x << 7, _x ^= _x >> 9;
}

// [l, r]
i64 rng(i64 l, i64 r) {
  assert(l <= r);
  return l + rng() % u64(r - l + 1);
}

// [l, r)
i64 randint(i64 l, i64 r) {
  assert(l < r);
  return l + rng() % u64(r - l);
}

// choose n numbers from [l, r) without overlapping
vector<i64> randset(i64 l, i64 r, i64 n) {
  assert(l <= r && n <= r - l);
  unordered_set<i64> s;
  for (i64 i = n; i; --i) {
    i64 m = randint(l, r + 1 - i);
    if (s.find(m) != s.end()) m = r - i;
    s.insert(m);
  }
  vector<i64> ret;
  for (auto& x : s) ret.push_back(x);
  sort(begin(ret), end(ret));
  return ret;
}

// [0.0, 1.0)
double rnd() { return rng() * 5.42101086242752217004e-20; }
// [l, r)
double rnd(double l, double r) {
  assert(l < r);
  return l + rnd() * (r - l);
}

template <typename T>
void randshf(vector<T>& v) {
  int n = v.size();
  for (int i = 1; i < n; i++) swap(v[i], v[randint(0, i + 1)]);
}

}  // namespace my_rand

using my_rand::randint;
using my_rand::randset;
using my_rand::randshf;
using my_rand::rnd;
using my_rand::rng;
#line 2 "modint/arbitrary-montgomery-modint.hpp"

#include <iostream>
using namespace std;

template <typename Int, typename UInt, typename Long, typename ULong, int id>
struct ArbitraryLazyMontgomeryModIntBase {
  using mint = ArbitraryLazyMontgomeryModIntBase;

  inline static UInt mod;
  inline static UInt r;
  inline static UInt n2;
  static constexpr int bit_length = sizeof(UInt) * 8;

  static UInt get_r() {
    UInt ret = mod;
    while (mod * ret != 1) ret *= UInt(2) - mod * ret;
    return ret;
  }
  static void set_mod(UInt m) {
    assert(m < (UInt(1u) << (bit_length - 2)));
    assert((m & 1) == 1);
    mod = m, n2 = -ULong(m) % m, r = get_r();
  }
  UInt a;

  ArbitraryLazyMontgomeryModIntBase() : a(0) {}
  ArbitraryLazyMontgomeryModIntBase(const Long &b)
      : a(reduce(ULong(b % mod + mod) * n2)){};

  static UInt reduce(const ULong &b) {
    return (b + ULong(UInt(b) * UInt(-r)) * mod) >> bit_length;
  }

  mint &operator+=(const mint &b) {
    if (Int(a += b.a - 2 * mod) < 0) a += 2 * mod;
    return *this;
  }
  mint &operator-=(const mint &b) {
    if (Int(a -= b.a) < 0) a += 2 * mod;
    return *this;
  }
  mint &operator*=(const mint &b) {
    a = reduce(ULong(a) * b.a);
    return *this;
  }
  mint &operator/=(const mint &b) {
    *this *= b.inverse();
    return *this;
  }

  mint operator+(const mint &b) const { return mint(*this) += b; }
  mint operator-(const mint &b) const { return mint(*this) -= b; }
  mint operator*(const mint &b) const { return mint(*this) *= b; }
  mint operator/(const mint &b) const { return mint(*this) /= b; }

  bool operator==(const mint &b) const {
    return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);
  }
  bool operator!=(const mint &b) const {
    return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);
  }
  mint operator-() const { return mint(0) - mint(*this); }
  mint operator+() const { return mint(*this); }

  mint pow(ULong n) const {
    mint ret(1), mul(*this);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul, n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const mint &b) {
    return os << b.get();
  }

  friend istream &operator>>(istream &is, mint &b) {
    Long t;
    is >> t;
    b = ArbitraryLazyMontgomeryModIntBase(t);
    return (is);
  }

  mint inverse() const {
    Int x = get(), y = get_mod(), u = 1, v = 0;
    while (y > 0) {
      Int t = x / y;
      swap(x -= t * y, y);
      swap(u -= t * v, v);
    }
    return mint{u};
  }

  UInt get() const {
    UInt ret = reduce(a);
    return ret >= mod ? ret - mod : ret;
  }

  static UInt get_mod() { return mod; }
};

// id に適当な乱数を割り当てて使う
template <int id>
using ArbitraryLazyMontgomeryModInt =
    ArbitraryLazyMontgomeryModIntBase<int, unsigned int, long long,
                                      unsigned long long, id>;
template <int id>
using ArbitraryLazyMontgomeryModInt64bit =
    ArbitraryLazyMontgomeryModIntBase<long long, unsigned long long, __int128_t,
                                      __uint128_t, id>;
#line 2 "prime/miller-rabin.hpp"

#line 4 "prime/miller-rabin.hpp"
using namespace std;

#line 8 "prime/miller-rabin.hpp"

namespace fast_factorize {

template <typename T, typename U>
bool miller_rabin(const T& n, vector<T> ws) {
  if (n <= 2) return n == 2;
  if (n % 2 == 0) return false;

  T d = n - 1;
  while (d % 2 == 0) d /= 2;
  U e = 1, rev = n - 1;
  for (T w : ws) {
    if (w % n == 0) continue;
    T t = d;
    U y = internal::modpow<T, U>(w, t, n);
    while (t != n - 1 && y != e && y != rev) y = y * y % n, t *= 2;
    if (y != rev && t % 2 == 0) return false;
  }
  return true;
}

bool miller_rabin_u64(unsigned long long n) {
  return miller_rabin<unsigned long long, __uint128_t>(
      n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
}

template <typename mint>
bool miller_rabin(unsigned long long n, vector<unsigned long long> ws) {
  if (n <= 2) return n == 2;
  if (n % 2 == 0) return false;

  if (mint::get_mod() != n) mint::set_mod(n);
  unsigned long long d = n - 1;
  while (~d & 1) d >>= 1;
  mint e = 1, rev = n - 1;
  for (unsigned long long w : ws) {
    if (w % n == 0) continue;
    unsigned long long t = d;
    mint y = mint(w).pow(t);
    while (t != n - 1 && y != e && y != rev) y *= y, t *= 2;
    if (y != rev && t % 2 == 0) return false;
  }
  return true;
}

bool is_prime(unsigned long long n) {
  using mint32 = ArbitraryLazyMontgomeryModInt<96229631>;
  using mint64 = ArbitraryLazyMontgomeryModInt64bit<622196072>;

  if (n <= 2) return n == 2;
  if (n % 2 == 0) return false;
  if (n < (1uLL << 30)) {
    return miller_rabin<mint32>(n, {2, 7, 61});
  } else if (n < (1uLL << 62)) {
    return miller_rabin<mint64>(
        n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
  } else {
    return miller_rabin_u64(n);
  }
}

}  // namespace fast_factorize

using fast_factorize::is_prime;

/**
 * @brief Miller-Rabin primality test
 */
#line 12 "prime/fast-factorize.hpp"

namespace fast_factorize {
using u64 = uint64_t;

template <typename mint, typename T>
T pollard_rho(T n) {
  if (~n & 1) return 2;
  if (is_prime(n)) return n;
  if (mint::get_mod() != n) mint::set_mod(n);
  mint R, one = 1;
  auto f = [&](mint x) { return x * x + R; };
  auto rnd_ = [&]() { return rng() % (n - 2) + 2; };
  while (1) {
    mint x, y, ys, q = one;
    R = rnd_(), y = rnd_();
    T g = 1;
    constexpr int m = 128;
    for (int r = 1; g == 1; r <<= 1) {
      x = y;
      for (int i = 0; i < r; ++i) y = f(y);
      for (int k = 0; g == 1 && k < r; k += m) {
        ys = y;
        for (int i = 0; i < m && i < r - k; ++i) q *= x - (y = f(y));
        g = gcd(q.get(), n);
      }
    }
    if (g == n) do
        g = gcd((x - (ys = f(ys))).get(), n);
      while (g == 1);
    if (g != n) return g;
  }
  exit(1);
}

using i64 = long long;

vector<i64> inner_factorize(u64 n) {
  using mint32 = ArbitraryLazyMontgomeryModInt<452288976>;
  using mint64 = ArbitraryLazyMontgomeryModInt64bit<401243123>;

  if (n <= 1) return {};
  u64 p;
  if (n <= (1LL << 30)) {
    p = pollard_rho<mint32, uint32_t>(n);
  } else if (n <= (1LL << 62)) {
    p = pollard_rho<mint64, uint64_t>(n);
  } else {
    exit(1);
  }
  if (p == n) return {i64(p)};
  auto l = inner_factorize(p);
  auto r = inner_factorize(n / p);
  copy(begin(r), end(r), back_inserter(l));
  return l;
}

vector<i64> factorize(u64 n) {
  auto ret = inner_factorize(n);
  sort(begin(ret), end(ret));
  return ret;
}

map<i64, i64> factor_count(u64 n) {
  map<i64, i64> mp;
  for (auto &x : factorize(n)) mp[x]++;
  return mp;
}

vector<i64> divisors(u64 n) {
  if (n == 0) return {};
  vector<pair<i64, i64>> v;
  for (auto &p : factorize(n)) {
    if (v.empty() || v.back().first != p) {
      v.emplace_back(p, 1);
    } else {
      v.back().second++;
    }
  }
  vector<i64> ret;
  auto f = [&](auto rc, int i, i64 x) -> void {
    if (i == (int)v.size()) {
      ret.push_back(x);
      return;
    }
    rc(rc, i + 1, x);
    for (int j = 0; j < v[i].second; j++) rc(rc, i + 1, x *= v[i].first);
  };
  f(f, 0, 1);
  sort(begin(ret), end(ret));
  return ret;
}

}  // namespace fast_factorize

using fast_factorize::divisors;
using fast_factorize::factor_count;
using fast_factorize::factorize;

/**
 * @brief 高速素因数分解(Miller Rabin/Pollard's Rho)
 * @docs docs/prime/fast-factorize.md
 */

const ll INF=1LL<<60;

void Solve(ll P){
    vector<ll>D=divisors(P);
    ll ans=INF;
    int L=D.size();
    for(int i=0;i<L;i++){
        ll w=D[i];
        if(w*w*w>P)break;
        ll Q=P/w;
        for(int j=i;j<L;j++){
            ll d=D[j];
            if(d*d>Q)break;
            if(Q%d!=0)continue;
            ll n=Q/d;
            chmin(ans,w+d+n);
        }
    }
    cout<<ans<<endl;
}

int main(){
    while(1){
        ll P;
        cin>>P;
        if(P==0)return 0;
        Solve(P);
    }
}
accuracy: 1
time_ms: 1630
memory_kb: 3940
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;

namespace noya2 {

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p){
    os << p.first << " " << p.second;
    return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p){
    is >> p.first >> p.second;
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v){
    for (auto &x : v) is >> x;
    return is;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u){
    cin >> t;
    in(u...);
}

void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u){
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}

template<typename T>
void out(const vector<vector<T>> &vv){
    int s = (int)vv.size();
    for (int i = 0; i < s; i++) out(vv[i]);
}

struct IoSetup {
    IoSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(7);
    }
} iosetup_noya2;

const int iinf = 1'000'000'007;
const long long linf = 2'000'000'000'000'000'000LL;
const long long mod998 =  998244353;
const long long mod107 = 1000000007;
const long double pi = 3.14159265358979323;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

void yes(){ cout << "Yes\n"; }
void no(){ cout << "No\n"; }
void YES(){ cout << "YES\n"; }
void NO(){ cout << "NO\n"; }
void yn(bool t){ t ? yes() : no(); }
void YN(bool t){ t ? YES() : NO(); }


unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){
    if (a == 0 || b == 0) return a + b;
    int n = __builtin_ctzll(a); a >>= n;
    int m = __builtin_ctzll(b); b >>= m;
    while (a != b) {
        int mm = __builtin_ctzll(a - b);
        bool f = a > b;
        unsigned long long c = f ? a : b;
        b = f ? b : a;
        a = (c - b) >> mm;
    }
    return a << min(n, m);
}

template<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }

long long sqrt_fast(long long n) {
    if (n <= 0) return 0;
    long long x = sqrt(n);
    while ((x + 1) * (x + 1) <= n) x++;
    while (x * x > n) x--;
    return x;
}

template<typename T> T floor_div(const T n, const T d) {
    assert(d != 0);
    return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);
}

template<typename T> T ceil_div(const T n, const T d) {
    assert(d != 0);
    return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);
}

template<typename T> void uniq(vector<T> &v){
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}

template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }

} // namespace noya2

#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)
#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;

namespace noya2{

/*　~ (. _________ . /)　*/

}

using namespace noya2;


namespace fast_factorize {

/*
    See : https://judge.yosupo.jp/submission/189742
*/

// ---- gcd ----

uint64_t gcd_stein_impl( uint64_t x, uint64_t y ) {
    if( x == y ) { return x; }
    const uint64_t a = y - x;
    const uint64_t b = x - y;
    const int n = __builtin_ctzll( b );
    const uint64_t s = x < y ? a : b;
    const uint64_t t = x < y ? x : y;
    return gcd_stein_impl( s >> n, t );
}

uint64_t gcd_stein( uint64_t x, uint64_t y ) {
    if( x == 0 ) { return y; }
    if( y == 0 ) { return x; }
    const int n = __builtin_ctzll( x );
    const int m = __builtin_ctzll( y );
    return gcd_stein_impl( x >> n, y >> m ) << ( n < m ? n : m );
}

// ---- is_prime ----

uint64_t mod_pow( uint64_t x, uint64_t y, uint64_t mod ) {
    uint64_t ret = 1;
    uint64_t acc = x;
    for( ; y; y >>= 1 ) {
        if( y & 1 ) {
            ret = __uint128_t(ret) * acc % mod;
        }
        acc = __uint128_t(acc) * acc % mod;
    }
    return ret;
}

bool miller_rabin( uint64_t n, const std::initializer_list<uint64_t>& as ) {
    return std::all_of( as.begin(), as.end(), [n]( uint64_t a ) {
        if( n <= a ) { return true; }

        int e = __builtin_ctzll( n - 1 );
        uint64_t z = mod_pow( a, ( n - 1 ) >> e, n );
        if( z == 1 || z == n - 1 ) { return true; }

        while( --e ) {
            z = __uint128_t(z) * z % n;
            if( z == 1 ) { return false; }
            if( z == n - 1 ) { return true; }
        }

        return false;
    });
}

bool is_prime( uint64_t n ) {
    if( n == 2 ) { return true; }
    if( n % 2 == 0 ) { return false; }
    if( n < 4759123141 ) { return miller_rabin( n, { 2, 7, 61 } ); }
    return miller_rabin( n, { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 } );
}

// ---- Montgomery ----

class Montgomery {
    uint64_t mod;
    uint64_t R;
public:
    Montgomery( uint64_t n ) : mod(n), R(n) {
       for( size_t i = 0; i < 5; ++i ) {
          R *= 2 - mod * R;
       }
    }

    uint64_t fma( uint64_t a, uint64_t b, uint64_t c ) const {
        const __uint128_t d = __uint128_t(a) * b;
        const uint64_t    e = c + mod + ( d >> 64 );
        const uint64_t    f = uint64_t(d) * R;
        const uint64_t    g = ( __uint128_t(f) * mod ) >> 64;
        return e - g;
    }

    uint64_t mul( uint64_t a, uint64_t b ) const {
        return fma( a, b, 0 );
    }
};

// ---- Pollard's rho algorithm ----

uint64_t pollard_rho( uint64_t n ) {
    if( n % 2 == 0 ) { return 2; }
    const Montgomery m( n );

    constexpr uint64_t C1 = 1;
    constexpr uint64_t C2 = 2;
    constexpr uint64_t M = 512;

    uint64_t Z1 = 1;
    uint64_t Z2 = 2;
retry:
    uint64_t z1 = Z1;
    uint64_t z2 = Z2;
    for( size_t k = M; ; k *= 2 ) {
        const uint64_t x1 = z1 + n;
        const uint64_t x2 = z2 + n;
        for( size_t j = 0; j < k; j += M ) {
            const uint64_t y1 = z1;
            const uint64_t y2 = z2;

            uint64_t q1 = 1;
            uint64_t q2 = 2;
            z1 = m.fma( z1, z1, C1 );
            z2 = m.fma( z2, z2, C2 );
            for( size_t i = 0; i < M; ++i ) {
                const uint64_t t1 = x1 - z1;
                const uint64_t t2 = x2 - z2;
                z1 = m.fma( z1, z1, C1 );
                z2 = m.fma( z2, z2, C2 );
                q1 = m.mul( q1, t1 );
                q2 = m.mul( q2, t2 );
            }
            q1 = m.mul( q1, x1 - z1 );
            q2 = m.mul( q2, x2 - z2 );

            const uint64_t q3 = m.mul( q1, q2 );
            const uint64_t g3 = gcd_stein( n, q3 );
            if( g3 == 1 ) { continue; }
            if( g3 != n ) { return g3; }

            const uint64_t g1 = gcd_stein( n, q1 );
            const uint64_t g2 = gcd_stein( n, q2 );

            const uint64_t C = g1 != 1 ? C1 : C2;
            const uint64_t x = g1 != 1 ? x1 : x2;
            uint64_t       z = g1 != 1 ? y1 : y2;
            uint64_t       g = g1 != 1 ? g1 : g2;

            if( g == n ) {
                do {
                    z = m.fma( z, z, C );
                    g = gcd_stein( n, x - z );
                } while( g == 1 );
            }
            if( g != n ) {
                return g;
            }

            Z1 += 2;
            Z2 += 2;
            goto retry;
        }
    }
}

void factorize_impl( uint64_t n, std::vector<uint64_t>& ret ) {
    if( n <= 1 ) { return; }
    if( is_prime( n ) ) { ret.push_back( n ); return; }

    const uint64_t p = pollard_rho( n );

    factorize_impl( p, ret );
    factorize_impl( n / p, ret );
}

std::vector<uint64_t> factorize( uint64_t n ) {
    std::vector<uint64_t> ret;
    factorize_impl( n, ret );
    std::sort( ret.begin(), ret.end() );
    return ret;
}

} // namespace fast_factorize

namespace noya2 {

std::vector<std::pair<long long, int>> factorize(long long n){
    std::vector<std::pair<long long, int>> ans;
    auto ps = fast_factorize::factorize(n);
    int sz = ps.size();
    for (int l = 0, r = 0; l < sz; l = r){
        while (r < sz && ps[l] == ps[r]) r++;
        ans.emplace_back(ps[l], r-l);
    }
    return ans;
}

std::vector<long long> divisors(long long n){
    auto ps = fast_factorize::factorize(n);
    int sz = ps.size();
    std::vector<long long> ans = {1};
    for (int l = 0, r = 0; l < sz; l = r){
        while (r < sz && ps[l] == ps[r]) r++;
        int e = r - l;
        int len = ans.size();
        ans.reserve(len*(e+1));
        long long mul = ps[l];
        while (true){
            for (int i = 0; i < len; i++){
                ans.emplace_back(ans[i]*mul);
            }
            if (--e == 0) break;
            mul *= ps[l];
        }
    }
    return ans;
}

std::vector<long long> divisors(const std::vector<std::pair<long long, int>> &pes){
    std::vector<long long> ans = {1};
    for (auto [p, e] : pes){
        int len = ans.size();
        ans.reserve(len*(e+1));
        long long mul = p;
        while (true){
            for (int i = 0; i < len; i++){
                ans.emplace_back(ans[i]*mul);
            }
            if (--e == 0) break;
            mul *= p;
        }
    }
    return ans;
}

bool is_prime(long long n){
    if (n <= 1) return false;
    return fast_factorize::is_prime(n);
}

} // namespace noya2

void solve(ll p){
    auto ds = divisors(p);
    sort(all(ds));
    ll ans = p+2;
    int sz = ds.size();
    ll lim = 100100;
    rep(i,sz){
        if (ds[i] >= lim) break;
        repp(j,i,sz){
            if (ds[i] > p/ds[j] + 1) break;
            if (ds[i]*ds[j] > p/ds[j] + 1) break;
            if (p % (ds[i] * ds[j]) != 0) continue;
            chmin(ans,ds[i]+ds[j]+p/(ds[i]*ds[j]));
        }
    }
    out(ans);
}

int main(){
    while(true){
        ll p; in(p);
        if (p == 0) break;
        solve(p);
    }
}
accuracy: 1
time_ms: 2430
memory_kb: 3940
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

constexpr ll INF = 4e18;
vector<ll> primes;
vector<ll> eratosthenes(ll n) {
    vector<bool> is_prime(n + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for (ll i = 2; i * i <= n; i++) {
        if (is_prime[i]) {
            for (ll j = 2 * i; j <= n; j += i) {
                is_prime[j] = false;
            }
        }
    }
    vector<ll> p;
    for (int i = 0; i <= n; i++) {
        if (is_prime[i]) {
            p.push_back(i);
        }
    }
    return p;
}

vector<ll> divisors(ll n) {
    vector<ll> divs = {1};
    for (auto p : primes) {
        if (p * p > n) {
            break;
        }
        if (n % p != 0) {
            continue;
        }

        int cnt = 0;
        while (n % p == 0) {
            n /= p;
            cnt++;
        }
        int siz = divs.size();
        for (int i = 0; i < siz; i++) {
            ll prod = 1;
            for (int c = 0; c < cnt; c++) {
                prod *= p;
                divs.push_back(divs[i] * prod);
            }
        }
    }

    if (n > 1) {
        int siz = divs.size();
        for (int i = 0; i < siz; i++) {
            divs.push_back(divs[i] * n);
        }
    }

    ranges::sort(divs);
    return divs;
}

void solve() {
    ll p;
    cin >> p;
    if (p == 0) {
        exit(0);
    }

    auto divs = divisors(p);
    ll ans    = INF;
    for (ll d1 : divs) {
        if (d1 * d1 * d1 > p) {
            break;
        }
        ll rem = p / d1;
        for (ll d2 : divs) {
            if (d1 * d2 * d2 > p) {
                break;
            }
            if (rem % d2 == 0) {
                ll d3  = rem / d2;
                ll sum = d1 + d2 + d3;
                ans    = min(ans, sum);
            }
        }
    }
    cout << ans << endl;
}

int main() {
    primes = eratosthenes(static_cast<ll>(sqrt(1e15)));
    while (true) {
        solve();
    }
}
accuracy: 1
time_ms: 3000
memory_kb: 25484
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

constexpr ll INF = 4e18;
vector<ll> primes;
vector<ll> eratosthenes(ll n) {
    vector<bool> is_prime(n + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for (ll i = 2; i * i <= n; i++) {
        if (is_prime[i]) {
            for (ll j = 2 * i; j <= n; j += i) {
                is_prime[j] = false;
            }
        }
    }
    vector<ll> p;
    for (int i = 0; i <= n; i++) {
        if (is_prime[i]) {
            p.push_back(i);
        }
    }
    return p;
}

vector<ll> divisors(ll n) {
    vector<ll> divs = {1};
    for (auto p : primes) {
        if (p * p > n) {
            break;
        }
        if (n % p != 0) {
            continue;
        }

        int cnt = 0;
        while (n % p == 0) {
            n /= p;
            cnt++;
        }
        int siz = divs.size();
        for (int i = 0; i < siz; i++) {
            ll prod = 1;
            for (int c = 0; c < cnt; c++) {
                prod *= p;
                divs.push_back(divs[i] * prod);
            }
        }
    }

    if (n > 1) {
        int siz = divs.size();
        for (int i = 0; i < siz; i++) {
            divs.push_back(divs[i] * n);
        }
    }

    ranges::sort(divs);
    return divs;
}

void solve() {
    ll p;
    cin >> p;
    if (p == 0) {
        exit(0);
    }

    auto divs = divisors(p);
    // ll lim1   = (ll) pow(p, 1.0 / 3.0) + 4;
    ll ans = INF;
    for (ll d1 : divs) {
        ll x = p / d1;
        if (d1 * d1 * d1 > p) {
            break;
        }
        for (ll d2 : divs) {
            if (d1 * d2 * d2 > p) {
                break;
            }

            if (x % d2 != 0) {
                continue;
            }
            ll d3  = x / d2;
            ll res = d1 + d2 + d3;
            ans    = min(ans, res);
        }
    }
    cout << ans << endl;
}

int main() {
    primes = eratosthenes(static_cast<ll>(sqrt(1e15)));
    while (true) {
        solve();
    }
}
accuracy: 1
time_ms: 2990
memory_kb: 25352
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

constexpr ll INF = 4e18;
vector<ll> primes;
vector<ll> eratosthenes(ll n) {
    vector<bool> is_prime(n + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for (ll i = 2; i * i <= n; i++) {
        if (is_prime[i]) {
            for (ll j = 2 * i; j <= n; j += i) {
                is_prime[j] = false;
            }
        }
    }
    vector<ll> p;
    for (int i = 0; i <= n; i++) {
        if (is_prime[i]) {
            p.push_back(i);
        }
    }
    return p;
}

vector<ll> divisors(ll n) {
    vector<ll> divs = {1};
    for (auto p : primes) {
        if (p * p > n) {
            break;
        }
        if (n % p != 0) {
            continue;
        }

        int cnt = 0;
        while (n % p == 0) {
            n /= p;
            cnt++;
        }
        int siz = divs.size();
        for (int i = 0; i < siz; i++) {
            ll prod = 1;
            for (int c = 0; c < cnt; c++) {
                prod *= p;
                divs.push_back(divs[i] * prod);
            }
        }
    }

    if (n > 1) {
        int siz = divs.size();
        for (int i = 0; i < siz; i++) {
            divs.push_back(divs[i] * n);
        }
    }

    ranges::sort(divs);
    return divs;
}

void solve() {
    ll p;
    cin >> p;
    if (p == 0) {
        exit(0);
    }

    auto divs = divisors(p);
    ll lim1   = (ll) pow(p, 1.0 / 3.0) + 4;
    ll ans    = INF;
    for (int i = 0; i < divs.size() && divs[i] < lim1; i++) {
        ll f1   = divs[i];
        ll rest = p / f1;
        ll lim2 = (ll) sqrt(rest) + 4;
        for (int j = 0; j < divs.size() && divs[j] < lim2; j++) {
            ll f2 = divs[j];
            if (rest % f2 == 0) {
                ll f3  = rest / f2;
                ll sum = f1 + f2 + f3;
                ans    = min(ans, sum);
            }
        }
    }
    cout << ans << endl;
}

int main() {
    primes = eratosthenes(static_cast<ll>(sqrt(1e15)));
    while (true) {
        solve();
    }
}
accuracy: 1
time_ms: 3000
memory_kb: 25356
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll llINF = (long long)4e18 + 22000020;

template<class T, class U>
bool chmin(T &a, const U &b) {
    return ((a > b) ? (a = b, true) : false);
}

vector<ll> primes;

vector<ll> eratosthenes(ll n) {
    vector<bool> isPrime(n + 1, true);
    isPrime[0] = false;
    isPrime[1] = false;

    for (ll i = 2; i * i <= n; i++) {
        if (isPrime[i]) {
            ll prod = i * 2;
            while (prod <= n) {
                isPrime[prod] = false;
                prod += i;
            }
        }
    }

    vector<ll> ret;
    for (int i = 0; i < isPrime.size(); i++) {
        if (isPrime[i]) ret.emplace_back(i);
    }
    return ret;
}

vector<ll> enum_divisors(ll n) {
    vector<ll> ret = {1};
    for (auto pr : primes) {
        if (pr * pr > n) break;
        if (n % pr != 0) continue;
        int cnt = 0;
        while (n % pr == 0) {
            n /= pr;
            cnt++;
        }
        int sz = ret.size();
        for (int i = 0; i < sz; i++) {
            ll prod = 1;
            for (int c = 0; c < cnt; c++) {
                prod *= pr;
                ret.emplace_back(ret[i] * prod);
            }
        }
    }

    if (n > 1) {
        int sz = ret.size();
        for (int i = 0; i < sz; i++) {
            ret.emplace_back(ret[i] * n);
        }
    }

    sort(ret.begin(), ret.end());
    return ret;
}


void solve() {
    ll p;
    cin >> p;
    if (p == 0) exit(0);
    auto divs = enum_divisors(p);

    ll limit1 = (ll)pow(p, 1.0 / 3.0) + 4;

    ll ans = llINF;
    for (int i = 0; i < divs.size() && divs[i] < limit1; i++) {
        ll f1 = divs[i];
        ll rest = p / f1;
        ll limit2 = (ll)sqrt(rest) + 4;
        for (int j = 0; j < divs.size() && divs[j] < limit2; j++) {
            ll f2 = divs[j];
            if (rest % f2 == 0) {
                ll f3 = rest / f2;
                ll sum = f1 + f2 + f3;
                chmin(ans, sum);
            }
        }
    }

    cout << ans << endl;
}

int main() {
    primes = move(eratosthenes((ll)sqrt(1e15) + 10));
    while (true) solve();
    
    return 0;
}
accuracy: 1
time_ms: 3030
memory_kb: 25484
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll llINF = (long long)4e18 + 22000020;

template<class T, class U>
bool chmin(T &a, const U &b) {
    return ((a > b) ? (a = b, true) : false);
}

vector<ll> primes;

vector<ll> eratosthenes(ll n) {
    vector<bool> isPrime(n + 1, true);
    isPrime[0] = false;
    isPrime[1] = false;

    for (ll i = 2; i * i <= n; i++) {
        if (isPrime[i]) {
            ll prod = i * 2;
            while (prod <= n) {
                isPrime[prod] = false;
                prod += i;
            }
        }
    }

    vector<ll> ret;
    for (int i = 0; i < isPrime.size(); i++) {
        if (isPrime[i]) ret.emplace_back(i);
    }
    return ret;
}

vector<ll> enum_divisors(ll n) {
    vector<ll> ret = {1};
    for (auto pr : primes) {
        if (pr * pr > n) break;
        if (n % pr != 0) continue;
        int cnt = 0;
        while (n % pr == 0) {
            n /= pr;
            cnt++;
        }
        int sz = ret.size();
        for (int i = 0; i < sz; i++) {
            ll prod = 1;
            for (int c = 0; c < cnt; c++) {
                prod *= pr;
                ret.emplace_back(ret[i] * prod);
            }
        }
    }

    if (n > 1) {
        int sz = ret.size();
        for (int i = 0; i < sz; i++) {
            ret.emplace_back(ret[i] * n);
        }
    }

    sort(ret.begin(), ret.end());
    return ret;
}


void solve() {
    ll p;
    cin >> p;
    if (p == 0) exit(0);
    auto divs = enum_divisors(p);

    ll limit1 = (ll)pow(p, 1.0 / 3.0) + 4;

    ll ans = llINF;
    for (int i = 0; i < divs.size() && divs[i] < limit1; i++) {
        ll f1 = divs[i];
        ll rest = p / f1;
        ll limit2 = (ll)sqrt(rest) + 4;
        for (int j = 0; j < divs.size() && divs[j] < limit2; j++) {
            ll f2 = divs[j];
            if (rest % f2 == 0) {
                ll f3 = rest / f2;
                ll sum = f1 + f2 + f3;
                chmin(ans, sum);
            }
        }
    }

    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    primes = move(eratosthenes((ll)sqrt(1e15) + 10));
    while (true) solve();
    
    return 0;
}
accuracy: 1
time_ms: 3060
memory_kb: 25508
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll INF = 4e18;

template <class T, class U>
bool chmin(T& a, const U& b) {
    return ((a > b) ? (a = b, true) : false);
}

vector<ll> primes;

vector<ll> eratosthenes(ll n) {
    vector<bool> isPrime(n + 1, true);
    isPrime[0] = false;
    isPrime[1] = false;

    for (ll i = 2; i * i <= n; i++) {
        if (isPrime[i]) {
            ll prod = i * 2;
            while (prod <= n) {
                isPrime[prod] = false;
                prod += i;
            }
        }
    }

    vector<ll> ret;
    for (int i = 0; i < isPrime.size(); i++) {
        if (isPrime[i]) {
            ret.emplace_back(i);
        }
    }
    return ret;
}

vector<ll> enum_divisors(ll n) {
    vector<ll> ret = {1};
    for (auto pr : primes) {
        if (pr * pr > n) {
            break;
        }
        if (n % pr != 0) {
            continue;
        }
        int cnt = 0;
        while (n % pr == 0) {
            n /= pr;
            cnt++;
        }
        int sz = ret.size();
        for (int i = 0; i < sz; i++) {
            ll prod = 1;
            for (int c = 0; c < cnt; c++) {
                prod *= pr;
                ret.emplace_back(ret[i] * prod);
            }
        }
    }

    if (n > 1) {
        int sz = ret.size();
        for (int i = 0; i < sz; i++) {
            ret.emplace_back(ret[i] * n);
        }
    }

    sort(ret.begin(), ret.end());
    return ret;
}


void solve() {
    ll p;
    cin >> p;
    if (p == 0) {
        exit(0);
    }
    auto divs = enum_divisors(p);

    ll limit1 = (ll) pow(p, 1.0 / 3.0) + 4;

    ll ans = INF;
    for (int i = 0; i < divs.size() && divs[i] < limit1; i++) {
        ll f1     = divs[i];
        ll rest   = p / f1;
        ll limit2 = (ll) sqrt(rest) + 4;
        for (int j = 0; j < divs.size() && divs[j] < limit2; j++) {
            ll f2 = divs[j];
            if (rest % f2 == 0) {
                ll f3  = rest / f2;
                ll sum = f1 + f2 + f3;
                chmin(ans, sum);
            }
        }
    }

    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    primes = move(eratosthenes((ll) sqrt(1e15) + 10));
    while (true) {
        solve();
    }
}
accuracy: 1
time_ms: 3060
memory_kb: 25508
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using u64 = uint64_t;

// --- factorize --------------------------------------------------------------
vector<pair<u64, uint32_t>> factorize(u64 n) {
    vector<pair<u64, uint32_t>> res;

    for (u64 p : {2, 3}) {
        uint32_t c = 0;
        while (n % p == 0) {
            ++c;
            n /= p;
        }
        if (c) {
            res.emplace_back(p, c);
        }
    }

    u64 k = 5, add = 2;
    while (k * k <= n) {
        uint32_t c = 0;
        while (n % k == 0) {
            n /= k;
            ++c;
        }
        if (c) {
            res.emplace_back(k, c);
        }
        k += add;
        add ^= 6; // 2 → 4 → 2 → 4 …
    }

    if (n > 1) {
        res.emplace_back(n, 1);
    }
    return res;
}

// --- helpers ----------------------------------------------------------------
inline u64 pow_u64(u64 x, unsigned e) {
    u64 r = 1;
    while (e--) {
        r *= x;
    }
    return r;
}

// --- divisor ----------------------------------------------------------------
vector<u64> divisor(u64 n) {
    auto f = factorize(n);

    size_t cap = 1;
    for (auto [_, c] : f) {
        cap *= (c + 1);
    }

    vector<u64> res;
    res.reserve(cap);
    res.push_back(1);

    for (auto [p, c] : f) {
        size_t len = res.size();
        u64 mul    = 1;
        for (uint32_t i = 1; i <= c; ++i) {
            mul *= p;
            for (size_t j = 0; j < len; ++j) {
                res.push_back(res[j] * mul);
            }
        }
        sort(res.begin(), res.end());
    }
    return res;
}

// --- solve ------------------------------------------------------------------
void solve(u64 n) {
    auto d  = divisor(n);
    u64 ans = n + 2;

    size_t x = 0;
    while (x < d.size() && d[x] * d[x] <= n) {
        ++x;
    }

    for (u64 a : d) {
        if (pow_u64(a, 3) > n) {
            break;
        }
        u64 m = n / a;

        while (x && pow_u64(d[x - 1], 2) > m) {
            --x;
        }

        size_t y = x;
        while (m % d[y - 1] != 0) {
            --y;
        }

        ans = min(ans, a + d[y - 1] + m / d[y - 1]);
    }
    cout << ans << '\n';
}

// --- main -------------------------------------------------------------------
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    u64 n;
    while (cin >> n) {
        if (n) {
            solve(n);
        }
    }
}
accuracy: 1
time_ms: 2400
memory_kb: 3684
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

// --- Miller–Rabin for 64bit ---
ll mod_mul(ll a, ll b, ll m) {
    __int128 t = (__int128)a * b;
    return (ll)(t % m);
}
ll mod_pow(ll a, ll e, ll m) {
    ll r = 1;
    while (e) {
        if (e & 1) r = mod_mul(r, a, m);
        a = mod_mul(a, a, m);
        e >>= 1;
    }
    return r;
}
bool isPrime(ll n) {
    if (n < 2) return false;
    for (ll p : {2,3,5,7,11,13,17,19,23,29,31,37}) {
        if (n%p==0) return n==p;
    }
    ll d = n-1, s = 0;
    while ((d&1)==0) { d>>=1; ++s; }
    for (ll a : {2,325,9375,28178,450775,9780504,1795265022}) {
        if (a % n == 0) continue;
        ll x = mod_pow(a, d, n);
        if (x==1 || x==n-1) continue;
        bool comp = true;
        for (int r=1; r<s; ++r) {
            x = mod_mul(x, x, n);
            if (x == n-1) { comp = false; break; }
        }
        if (comp) return false;
    }
    return true;
}

// --- Pollard's Rho ---
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll pollards_rho(ll n) {
    if (n%2==0) return 2;
    if (n%3==0) return 3;
    ll c = uniform_int_distribution<ll>(1, n-1)(rng);
    ll x = uniform_int_distribution<ll>(0, n-1)(rng);
    ll y = x;
    ll d = 1;
    auto f = [&](ll v){ return (mod_mul(v,v,n) + c) % n; };
    while (d==1) {
        x = f(x);
        y = f(f(y));
        d = gcd<ll>(llabs(x-y), n);
        if (d==n) return pollards_rho(n);
    }
    return d;
}

void factor(ll n, vector<ll> &out) {
    if (n==1) return;
    if (isPrime(n)) {
        out.push_back(n);
    } else {
        ll d = pollards_rho(n);
        factor(d, out);
        factor(n/d, out);
    }
}

// 再帰的に約数を生成
void gen_divs(int i, ll cur,
              const vector<pair<ll,int>> &fac,
              vector<ll> &divs) {
    if (i == (int)fac.size()) {
        divs.push_back(cur);
        return;
    }
    ll p = fac[i].first;
    int e = fac[i].second;
    for (int k = 0; k <= e; ++k) {
        gen_divs(i+1, cur, fac, divs);
        cur *= p;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while (true) {
        ll p;
        if (!(cin >> p)) break;
        if (p == 0) break;

        // 素因数分解
        vector<ll> tmp;
        factor(p, tmp);
        sort(tmp.begin(), tmp.end());
        vector<pair<ll,int>> fac;
        for (ll x : tmp) {
            if (fac.empty() || fac.back().first != x)
                fac.emplace_back(x, 1);
            else
                fac.back().second++;
        }

        // 全約数列挙
        vector<ll> divs;
        gen_divs(0, 1, fac, divs);
        sort(divs.begin(), divs.end());

        ll ans = LLONG_MAX;
        int D = divs.size();

        // w <= d <= h となるよう二重ループ
        for (int i = 0; i < D; ++i) {
            ll w = divs[i];
            ll q = p / w;
            for (int j = i; j < D; ++j) {
                ll d = divs[j];
                if (d * d > q) break;           // d > sqrt(q) は h<d になるので意味なし
                if (q % d) continue;
                ll h = q / d;
                if (h < d) continue;           // w<=d<=h を維持
                ans = min(ans, w + d + h);
            }
        }

        cout << ans << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 3540
memory_kb: 3648
final_rank: 6

code_snippet: #pragma region Macros
#include <bits/stdc++.h>

using namespace std;

using lint = long long;

using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define EPS 1e-8
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define log(x) cout << x << endl
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;
#define logy(bool)         \
  if (bool) {              \
    cout << "Yes" << endl; \
  } else {                 \
    cout << "No" << endl;  \
  }

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion

__int128_t mod_pow(__int128_t a, long long n, long long m) {
  __int128_t res = 1;
  a %= m;
  while (n) {
    if (n & 1) res = (res * a) % m;
    a = (a * a) % m;
    n >>= 1;
  }
  return res;
}

constexpr long long MR[] = {2, 7, 61};
constexpr long long MRl[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};

bool Miller_Rabin(long long n) {
  long long s = 0;
  long long d = n - 1;
  while ((d & 1) == 0) {
    s++;
    d >>= 1;
  }
  for (int i = 0; i < 3; i++) {
    if (n <= MR[i]) return true;
    __int128_t x = mod_pow(MR[i], d, n);
    if (x != 1) {
      bool ok = false;
      for (int t = 0; t < s; t++) {
        if (x == n - 1) {
          ok = true;
          break;
        }
        x = x * x % n;
      }
      if (!ok) return false;
    }
  }
  return true;
}

bool Miller_Rabinl(long long n) {
  long long s = 0;
  long long d = n - 1;
  while ((d & 1) == 0) {
    s++;
    d >>= 1;
  }
  for (int i = 0; i < 7; i++) {
    if (n <= MRl[i]) return true;
    __int128_t x = mod_pow(MRl[i], d, n);
    if (x != 1) {
      bool ok = false;
      for (int t = 0; t < s; t++) {
        if (x == n - 1) {
          ok = true;
          break;
        }
        x = x * x % n;
      }
      if (!ok) return false;
    }
  }
  return true;
}

bool brute_force(long long n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}

bool is_prime(long long n) {
  if (n == 2) return true;
  if ((n & 1) == 0 or n < 2) return false;
  if (n < 1000) return brute_force(n);
  if (n < 4759123141LL) {
    return Miller_Rabin(n);
  }
  return Miller_Rabinl(n);
}

long long find_prime_factor(long long n) {
  if ((n & 1) == 0) return 2;
  long long m = int64_t(powl(n, 0.125)) + 1;
  for (int i = 1; i < n; i++) {
    long long y = 0;
    long long g = 1;
    long long q = 1;
    long long r = 1;
    long long k = 0;
    long long ys = 0;
    long long x = 0;
    while (g == 1) {
      x = y;
      while (k < 3ll * r / 4) {
        y = (__int128_t(y) * y + i) % n;
        k++;
      }
      while (k < r and g == 1) {
        ys = y;
        for (int j = 0; j < min(m, r - k); j++) {
          y = (__int128_t(y) * y + i) % n;
          q = (__int128_t(q) * abs(x - y)) % n;
        }
        g = gcd(q, n);
        k += m;
      }
      k = r;
      r <<= 1;
    }
    if (g == n) {
      g = 1;
      y = ys;
      while (g == 1) {
        y = (__int128_t(y) * y + i) % n;
        g = gcd(abs(x - y), n);
      }
    }
    if (g == n) continue;
    if (is_prime(g)) return g;
    if (is_prime(n / g)) return n / g;
    return find_prime_factor(g);
  }
  return -1;
}

vector<long long> factorize(long long n, bool set = false) {
  vector<long long> res;
  while (!is_prime(n) and n > 1) {
    long long p = find_prime_factor(n);
    if (set) res.emplace_back(p);
    while (n % p == 0) {
      n /= p;
      if (!set) res.emplace_back(p);
    }
  }
  if (n > 1) {
    res.emplace_back(n);
  }
  sort(res.begin(), res.end());
  return res;
}

vector<long long> enumerate_divisors(long long n, bool sorted_result = false) {
  vector<long long> res = {1};
  long long before = -1;
  long long mul;
  int siz_before = 1;
  for (const long long p : factorize(n)) {
    mul = (p == before ? mul * p : p);
    int siz = (p == before ? siz_before : int(res.size()));
    for (int i = 0; i < siz; i++) {
      res.emplace_back(res[i] * mul);
    }
    before = p;
    siz_before = siz;
  }
  if (sorted_result) sort(res.begin(), res.end());
  return res;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  long long p;
  while (1) {
    long long res = LLONG_MAX;
    in(p);
    if (p == 0) break;
    for (const long long w : enumerate_divisors(p)) {
      long long dh = p / w;
      for (const long long d : enumerate_divisors(dh)) {
        long long h = dh / d;
        chmin(res, w + d + h);
      }
    }

    out(res);
  }
}
accuracy: 1
time_ms: 6240
memory_kb: 4204
final_rank: 16