#line 2 "header.hpp"

//%snippet.set('header')%
//%snippet.fold()%
#ifndef HEADER_H
#define HEADER_H

// template version 2.0
using namespace std;
#include <bits/stdc++.h>

// varibable settings
template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i)
#define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(...) make_tuple(__VA_ARGS__)
#define ub upper_bound
#define lb lower_bound
#define lpos(A, x) (lower_bound(all(A), x) - A.begin())
#define upos(A, x) (upper_bound(all(A), x) - A.begin())
template <class T, class U> inline void chmax(T &a, const U &b) { if ((a) < (b)) (a) = (b); }
template <class T, class U> inline void chmin(T &a, const U &b) { if ((a) > (b)) (a) = (b); }
template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); }

template <class T> T cdiv(T a, T b){ assert(a >= 0 && b > 0); return (a+b-1)/b; }

#define is_in(x, a, b) ((a) <= (x) && (x) < (b))
#define uni(x) sort(all(x)); x.erase(unique(all(x)), x.end())
#define slice(l, r) substr(l, r - l)

typedef long long ll;
typedef long double ld;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;

template <typename T>
using PQ = priority_queue<T, vector<T>, greater<T>>;
void check_input() { assert(cin.eof() == 0); int tmp; cin >> tmp; assert(cin.eof() == 1); }

#if defined(PCM) || defined(LOCAL)
#else
#define dump(...) ;
#define dump_1d(...) ;
#define dump_2d(...) ;
#define cerrendl ;
#endif

#endif /* HEADER_H */
//%snippet.end()%
#line 2 "solve.cpp"
template<class T=ll> using vec = vector<T>;
struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast;


int solve() {
    ll n;cin>>n;
    dump(n);

    ll mul = 3;
    for(ll a = 1; mul < n; a++){
        ll rem = n - mul;
        dump(a, rem);
        int b = 0;
        while (rem % 5 == 0){
            rem /= 5;
            b++;
        }
        if (rem == 1 && b >= 1){
            cout << a << " " << b << endl;
            return 0;
        }
        mul *= 3;
    }
    cout << -1 << endl;

    return 0; 
}


int main(){/*{{{*/
    solve();

    #if defined(PCM) || defined(LOCAL)
    check_input();
    #endif

    return 0;
}/*}}}*/

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

#define ffor(i, a, b) for (int i = a ; i <= (int) b ; i++) 
#define rfor(i, a, b) for (int i = (int) a ; i >= b ; i--)  

#define vec vector 
#define PB push_back 

#define MP make_pair 
#define MT make_tuple 
#define F first 
#define S second 

using ll = long long int; // watch out for overflows in your code
using pii = pair<int,int>;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void debug() { cout << "\n"; };
template <typename T, typename... Targs> 
void debug(T t, Targs... args) {
    cout << t << " ";
    debug(args...);
}

template <typename T1, typename T2> 
inline ostream& operator << (ostream& os, const pair<T1,T2>& p) {
    return os << "(" << p.F << ", " << p.S << ")";
}

template <typename T> 
inline ostream& operator << (ostream& os, const vec<T>& v) {
    for(auto& t : v) {
        os << t << " ";
    }
    return os;
}

template <typename T> 
inline ostream& operator << (ostream& os, const vec<vec<T>>& m) {
    for(auto& v : m) {
        for(auto& t : v) {
            os << t << " ";
        }
        os << "\n";
    }
    return os;
}

template <typename T> 
inline ostream& operator << (ostream& os, const set<T>& v) {
    for(auto& t : v) {
        os << t << " ";
    }
    return os;
}

template <typename K, typename V> 
inline ostream& operator << (ostream& os, const map<K,V>& m) {
    for(auto& p : m) {
        os << p.F << " -> " << p.S << "\n";
    }
    return os;
}

const ll INF = 9 * 1e18;

int N;
vec<ll> lo, hi;

ll dist(ll st, ll fn, ll x) {
    return abs(x-st) + abs(st-fn);
}

vec<vec<ll>> dp;

void compute_dp() {
    dp.assign(N+1, vec<ll>(2,0));
    ffor(c, 1, N) {
        if(lo[c] == lo[c-1] && hi[c] == hi[c-1]) {
            dp[c][0] = dp[c-1][0];
            dp[c][1] = dp[c-1][1];
        }
        else {
            dp[c][0] = min(dp[c-1][0] + dist(hi[c], lo[c], lo[c-1]), dp[c-1][1] + dist(hi[c], lo[c], hi[c-1]));
            dp[c][1] = min(dp[c-1][0] + dist(lo[c], hi[c], lo[c-1]), dp[c-1][1] + dist(lo[c], hi[c], hi[c-1]));
        }
        //debug("c=", c, "dp(c,0)=", dp[c][0], "dp(c,1)=", dp[c][1]);
    }
}

////////////////////////////////////////////////////////////////////////

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

    cin >> N;
    lo.assign(N+1, INF);
    hi.assign(N+1, -INF);
    lo[0] = hi[0] = 0;
    ffor(i, 1, N) {
        ll x; int c;
        cin >> x >> c;
        lo[c] = min(lo[c], x);
        hi[c] = max(hi[c], x);
    }

    ffor(c, 1, N) {
        if(lo[c] == INF) lo[c] = lo[c-1];
        if(hi[c] == -INF) hi[c] = hi[c-1];
    }

    //debug("lo=", lo);
    //debug("hi=", hi);

    compute_dp();
    cout << min(dp[N][0] + abs(lo[N]), dp[N][1] + abs(hi[N])) << "\n";
}

