//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Int64 n = Convert.ToInt64(Console.ReadLine());
            Int64 k = Convert.ToInt64(Console.ReadLine());
            Int64 A = Convert.ToInt64(Console.ReadLine());
            Int64 B = Convert.ToInt64(Console.ReadLine());

            Int64 cost = 0;

            while(n > 1) {
                if(n%k == 0 && k > 1) {
                    if (B < (A * (n - n/k))) {
                        cost += B;
                        n /= k;
                    }
                    else {
                        cost += (A * (n - n/k));
                        n /= k;
                    }
                }
                else {
                    if(k > 1) {
                        if(n - (n%k) == 0) {
                            cost += (A * (n%k - 1));
                            n -= (n%k + 1);
                        }
                        else {
                            cost += (A * (n%k));
                            n -= (n%k);
                        }
                    }
                    else {
                        cost += (A * (n-1));
                        n = 1;
                    }
                }
            }

            Console.WriteLine(cost);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Acyclic
{
    class Apop
    {
        public static void Main()
        {
            ulong n = UInt64.Parse(Console.ReadLine());
            ulong k = UInt64.Parse(Console.ReadLine());
            ulong A = UInt64.Parse(Console.ReadLine());
            ulong B = UInt64.Parse(Console.ReadLine());

            ulong koszt = 0;
            ulong Akoszt = 0;
            ulong Bkoszt = 0;
            if (k<=1) koszt += A * (n - 1);
            else
            {
                while(n!=1)
                {
                    if(n<k)
                    {
                        Akoszt += (n - 1);
                        break;
                    }
                    if(n%k==0)
                    {
                        ulong d = n / k;
                        ulong a = A * (n - d);
                        if (a < B) Akoszt += n - d;
                        else Bkoszt++;
                        n = d;
                    }
                    else
                    {
                        ulong x = n % k;
                        n -= x;
                        Akoszt += x;
                    }
                }

                koszt = A * Akoszt + B * Bkoszt;
            }

            Console.WriteLine(koszt);
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = int.Parse(Console.ReadLine());
            int div = int.Parse(Console.ReadLine());
            int subCost = int.Parse(Console.ReadLine());
            int divCost = int.Parse(Console.ReadLine());
            BigInteger cost = 0;
            if (div > 1)
            {
                while (x >= 1)
                {
                    if (x == 1)
                    {
                        cost += subCost;
                        break;
                    }
                    int rem = x % div;
                    if (rem == 0)
                    {
                        BigInteger sC = (BigInteger)subCost * (BigInteger)(x - x / div);
                        x /= div;
                        if (divCost < sC)
                        {
                            cost += divCost;
                        }
                        else
                        {
                            cost += sC;
                        }
                    }
                    else
                    {
                        cost += (BigInteger)subCost * (BigInteger)rem;
                        x -= rem;
                    }
                }
                cost -= subCost;
            }
            else
            {
                cost = (BigInteger)subCost * (BigInteger)(x - 1);
            }
            Console.WriteLine(cost);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace Quserty2
{
    public class Program
    {
        static ulong totalPrice = 0;

        static void Main(string[] args)
        {
            ulong n, k, A, B;

            n = ulong.Parse(Console.ReadLine());
            k = ulong.Parse(Console.ReadLine());
            A = ulong.Parse(Console.ReadLine());
            B = ulong.Parse(Console.ReadLine());

            if (k == 1)
            {
                Console.WriteLine((n - 1) * A);
                return;
            }

            ulong last = 0;

            uint index = 1;
            if (n / k < k)
            {
                while (true)
                {
                    var current = k * index;
                    if (current > n)
                        break;
                    last = current;
                    index++;
                }
            }
            else
            {
                var n2 = n;
                while (true)
                {
                    if (n2 % k == 0)
                    {
                        last = n2;
                        break;
                    }

                    if (n2 <= 1)
                    {
                        break;
                    }

                    --n2;
                }
            }

            if (last == 0)
            {
                Console.WriteLine((n - 1) * A);
                return;
            }

            totalPrice += (n - last) * A;
            n = last;

            for (;;)
            {
                if (n == 1)
                    break;

                if (last <= 1)
                {
                    break;
                }

                if (n < last)
                {
                    last -= k;
                    continue;
                }

                totalPrice += (n - last) * A;
                n = last;

                var divisionPrice = B;
                var minusPrice = (n - n / k) * A;
                if (divisionPrice < minusPrice)
                {
                    totalPrice += divisionPrice;
                    var divisionResult = n / k;
                    n = divisionResult;
                    var numberOfkTimesInDivisionResult = (last - n) / k;
                    last -= numberOfkTimesInDivisionResult * k;
                    continue;
                }
                else
                {
                    totalPrice += (n - 1) * A;
                    Console.WriteLine(totalPrice);
                    return;
                }
            }

            if (n > 1)
            {
                totalPrice += (n - 1) * A;
            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;
class Program
    {
        static void Main(string[] args)
        {
            long x, k, a, b;
            {
                string[] input = Console.In.ReadToEnd().Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                x = long.Parse(input[0]);
                k = long.Parse(input[1]);
                a = long.Parse(input[2]);
                b = long.Parse(input[3]);
            }
            long result = 0L;
            for (long smllr; x > 1L;)
            {
                if ((smllr = x % k) == 0L)
                {
                    if (((x - (smllr = x / k)) * a) <= b) { result += a * (x - 1L); x = 1L; break; }
                    else { x = smllr; result += b; }
                }
                else { result += smllr * a; x -= smllr; }
            }
            Console.WriteLine(x == 0L ? result - a : result);
        }
    }

using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharp
{
    class _940B
    {
        public static void Main()
        {
            long n = long.Parse(Console.ReadLine());
            long k = long.Parse(Console.ReadLine());
            long a = long.Parse(Console.ReadLine());
            long b = long.Parse(Console.ReadLine());

            var queue = new Queue<long>(new long[] { n });
            var amount = new Dictionary<long, long> { { n, 0 } };

            while (queue.Any())
            {
                long x = queue.Dequeue();

                if (!amount.ContainsKey(1) || amount[x] + a * (x - 1) < amount[1])
                {
                    amount[1] = amount[x] + a * (x - 1);
                }

                long mod = x % k;
                long div = x / k;

                if (mod > 0)
                {
                    if (!amount.ContainsKey(x - mod) || amount[x] + a * mod < amount[x - mod])
                    {
                        queue.Enqueue(x - mod);
                        amount[x - mod] = amount[x] + a * mod;
                    }
                }
                else
                {
                    if (!amount.ContainsKey(div) || amount[x] + b < amount[div])
                    {
                        queue.Enqueue(div);
                        amount[div] = amount[x] + b;
                    }
                }
            }

            Console.WriteLine(amount[1]);
        }
    }
}

using System;
using System.IO;
using System.Text;

namespace Our_Tanya_is_Crying_Out_Loud
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            writer.WriteLine(Solve(args));
            writer.Flush();
        }

        private static long Solve(string[] args)
        {
            long n = Next(), k = Next(), A = Next(), B = Next();

            if (k == 1)
                return (n - 1)*A;

            long ans = 0;
            while (n >= k)
            {
                long x = n/k;
                long m = x*k;
                long delta = n - m;
                ans += delta*A;
                n = m;
                x = n/k;
                ans += Math.Min(B, (n - x)*A);
                n = x;
            }

            ans += (n - 1)*A;

            return ans;
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeForces
{
   class B
   {
      static void Main(string[] args)
      {
         long n = long.Parse(Console.ReadLine());
         long k = long.Parse(Console.ReadLine());

         long A = long.Parse(Console.ReadLine());
         long B = long.Parse(Console.ReadLine());

         long X = n;

         long ans = 0;
         if(k==1||k>n){
             ans = ((n-1) * A);
             Console.WriteLine(ans);
             return;
           }

         while(X>1){
           if(X%k==0){
             long val = X/k;
             long diff = X-val;
             if(diff*A>B){
               ans += B;
               X /= k;
             }else{
               ans += (A*diff);
               X -= diff;
             }
           }else{
               if(X>k){
                 long mod = X%k;
                 ans += (A * mod);
                 X = X-mod;
               }else{
                 ans += (X-1)*A;
                 break;
               }
           }
         }

         Console.WriteLine(ans);
      }
   }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
//using tuple = System.IComparable;
//using bsearchTx = System.Int32;

public sealed class Solution : Helper
{
    public void __stdioMain()
    {
        checked
        {
            //
            var n = readInt32();
            var k = readInt32();
            long a = readInt32();
            long b = readInt32();
            //
            long res = 0;
            if (k == 1)
            {
                write((n - 1) * a);
                return;
            }
            while (n > 1)
            {
                if (n % k == 0)
                {

                    {
                        var costa = a * (n - (n / k));
                        if (costa < b)
                        {
                            res += costa;
                        }
                        else
                        {
                            res += b;
                        }
                        n = n / k;
                    }

                }
                else
                {
                    var rem = n % k;
                    n -= rem;
                    assert(n % k == 0);
                    if (n == 0)
                    {
                        n++;
                        rem--;
                    }
                    res += rem * a;
                }
            }
            assert(n == 1);
            write(res);
        }
    }
    //
}

// additional plugable library
public partial class Helper
{

    [DebuggerDisplay("{Value}")]
    internal class Accumulator<T> where T : struct
    {
        Func<T, T, T> merge; public T? ValueNullable { get; private set; }
        public Accumulator(Func<T, T, T> merge_CHECKED) { this.merge = merge_CHECKED; }
        public Accumulator<T> Add(T value) { ValueNullable = ValueNullable.HasValue ? merge(ValueNullable.Value, value) : value; return this; }
    }
    //
    static bool MULTI_TCs = false;
    #region STDIO
#if !DEBUG
    static void Main()
    {
        Console.SetIn(new System.IO.StreamReader(new System.IO.BufferedStream(Console.OpenStandardInput())));
        Console.SetOut(new System.IO.StreamWriter(new System.IO.BufferedStream(Console.OpenStandardOutput())));
        var cnt = MULTI_TCs ? readInt32() : 1;
        for (var i = 1; i <= cnt; i++) new Solution().__stdioMain();
        Console.Out.Flush();
    }
#endif
    // OUTput
    internal string autoSeparator;
    internal void writeLine() { Console.WriteLine(); }
    internal void writeSeparator() { if (!string.IsNullOrEmpty(autoSeparator))Console.Write(autoSeparator); }
    internal void write(char c) { Console.Write(c); writeSeparator(); }
    internal void write(string s) { Console.Write(s); writeSeparator(); }
    internal void write(long i64) { Console.Write(i64); writeSeparator(); }
    internal void write(double dec) { Console.Write(dec); writeSeparator(); }
    // INput
    static void EOF() { throw new System.IO.EndOfStreamException(); }
    static IEnumerable<char> stream() { while (true) { var iread = Console.Read(); if (iread < 0) break; yield return (char)iread; } }
    static bool isWhitespace(char c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t'; }
    static System.Text.StringBuilder sb = new System.Text.StringBuilder(30);
    internal static string readWordString()
    {
        sb.Clear();
        var iter = stream().SkipWhile(isWhitespace).TakeWhile(c => !isWhitespace(c));
        foreach (var c in iter) sb.Append(c);
        if (sb.Length == 0) EOF(); return sb.ToString();
    }
    internal static long readLong64() { return long.Parse(readWordString()); }
    internal static int readInt32() { return Convert.ToInt32(readLong64()); }
    #endregion
}
static partial class Extension
{
    //
}

#region Core helper library
[DebuggerNonUserCode]
public partial class Helper
{
    internal static IEnumerable<int> seq(int low, int high, int step = 1) { checked { for (var i = low; i <= high; i += step) yield return i; } }
    internal static IEnumerable<int> rev(int low, int high, int stepPositive = 1) { checked { for (var i = high; i >= low; i -= stepPositive) yield return i; } }
    internal static T[] collect<T>(params T[] elemS) { return elemS; }
    internal static T? optional<T>(bool isNotNull, T value) where T : struct { return isNotNull ? new T?(value) : null; }
    internal static T identity<T>(T obj) { return obj; }
    internal static void assert(bool truthy) { if (!truthy) throw new PlatformNotSupportedException("ASSERT"); }
    internal static void assert<T>(T actual, T expected) { if (!Equals(actual, expected)) { Debug.WriteLine("SALAH {0} / BENAR {1}", actual, expected); assert(false); } }
    internal static List<T> ll<T>(T emptyExample) { return new List<T>(); }
}
[DebuggerNonUserCode]
static partial class Extension
{
    internal static IEnumerable<T> Select<T>(this IEnumerable<T> stream, Func<T> getter) { return stream.Select(_ => getter()); }
    internal static void elect<T>(this IEnumerable<T> stream, Action<T> act) { foreach (var elem in stream) act(elem); }
    internal static IEnumerable<T> Select<T>(this int count, Func<T> getter) { for (var i = 0; i < count; i++) yield return getter(); }
    internal static void elect(this int count, Action act) { for (var i = 0; i < count; i++) act(); }
    internal static int GetUpperBound<T>(this IList<T> list) { return list.Count - 1; }
    internal static int getUpperBound(this int count) { return count - 1; }
    internal static int getUpperBound(this System.Collections.IList list) { return list.Count - 1; }
    internal static IEnumerable<int> indices<T>(this IList<T> list) { for (var i = 0; i < list.Count; i++) yield return i; }
    internal static List<KeyValuePair<int, T>> ToKvpList<T>(this IEnumerable<T> list) { return list.Select((elem, idx) => new KeyValuePair<int, T>(idx, elem)).ToList(); }
    internal static T? firstOrNullable<T>(this IEnumerable<T> stream) where T : struct { foreach (var val in stream) return val; return null; }
    internal static T? elementAtOrNullable<T>(this IList<T> list, int index) where T : struct { if (index < 0 || index >= list.Count) return null; return list[index]; }
    internal static IEnumerable<T> skip<T>(this IList<T> list, int count) { for (var i = count; i < list.Count; i++) yield return list[i]; }
    internal static IEnumerable<T> reverseView<T>(this IList<T> list) { for (var i = list.Count - 1; i >= 0; i--) yield return list[i]; }
    internal static T? AggregateOrNullable<T>(this IEnumerable<T> stream, Func<T, T, T> merge_CHECKED) where T : struct
    { var it = stream.GetEnumerator(); if (!it.MoveNext()) return null; var res = it.Current; while (it.MoveNext()) res = merge_CHECKED(res, it.Current); return res; }
    //
    internal static int? explainSearchIndex(this int rawIndex) { if (rawIndex < 0) return null; return rawIndex; }
    internal static List<T> ll<T>(this IEnumerable<T> stream) { return stream.ToList(); }
    internal static string stringify(this IEnumerable<char> stream) { return new string(stream.ToArray()); }
    internal static T[] sort<T>(this T[] arr) { Array.Sort(arr); return arr; }
    internal static List<T> sort<T>(this List<T> lst) { lst.Sort(); return lst; }
    internal static T removeOrDequeue<T>(this SortedSet<T> pq, bool minimum = true) { var t = minimum ? pq.Min : pq.Max; pq.Remove(t); return t; }
}
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {
            long n = Convert.ToInt64(Console.ReadLine()), k = Convert.ToInt64(Console.ReadLine()), a = Convert.ToInt64(Console.ReadLine()), b = Convert.ToInt64(Console.ReadLine());
            long  ans = 0;
            if (k != 1)
            {
                while (n != 1)
                {
                    if (n % k != 0 && n / k != 0)
                    {
                        ans += n % k * a; n -= n % k;
                    }
                    else if (n < k)
                    {
                        ans += ((n - 1) * a);
                        n = 1;
                    }
                    else
                    {
                        if (b <= a * (n - (n / k)))
                        {
                            ans += b; n /= k;
                        }
                        else
                        {
                            ans += a * (n - (n / k)); n /= k;
                        }
                    }

                }

                Console.WriteLine(ans);
            }
            else
            {
                ans = a * (n - 1);
                Console.WriteLine(ans);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contest
{
    class Scanner
    {
        private string[] line = new string[0];
        private int index = 0;
        public string Next()
        {
            if (line.Length <= index)
            {
                line = Console.ReadLine().Split(' ');
                index = 0;
            }
            var res = line[index];
            index++;
            return res;
        }
        public int NextInt()
        {
            return int.Parse(Next());
        }
        public long NextLong()
        {
            return long.Parse(Next());
        }
        public string[] Array()
        {
            line = Console.ReadLine().Split(' ');
            index = line.Length;
            return line;
        }
        public int[] IntArray()
        {
            return Array().Select(int.Parse).ToArray();
        }
        public long[] LongArray()
        {
            return Array().Select(long.Parse).ToArray();
        }
    }

    class Program
    {
        private long N, K, A, B;
        private void Scan()
        {
            var sc = new Scanner();
            N = sc.NextInt();
            K = sc.NextInt();
            A = sc.NextInt();
            B = sc.NextInt();
        }

        public void Solve()
        {
            Scan();
            long ans = 0;
            if (K == 1)
            {
                Console.WriteLine((N - 1) * A);
                return;
            }
            while (N > 1)
            {
                if (N % K == 0)
                {
                    long aa = (N / K * (K - 1)) * A;
                    ans += Math.Min(aa, B);
                    N /= K;
                }
                else
                {
                    if (N / K == 0)
                    {
                        ans += (N - 1) * A;
                        N = 1;
                    }
                    else
                    {
                        ans += (N % K) * A;
                        N -= N % K;
                    }
                }
            }
            Console.WriteLine(ans);
        }

        static void Main(string[] args)
        {
            new Program().Solve();
        }
    }
}

using System;

namespace _940B
{
    public class Program
    {
        static void Main(string[] args)
        {
            var n = ulong.Parse(Console.ReadLine());
            var k = ulong.Parse(Console.ReadLine()); //გამყოფი
            var a = ulong.Parse(Console.ReadLine()); //გამოკლების ფასი
            var b = ulong.Parse(Console.ReadLine()); //გაყოფის ფასი
            ulong totalPrice = 0;

            if (k == 1 || n / k < 1)
            {
                Console.WriteLine((n - 1) * a);
                return;
            }

            while (n > 1)
            {
                if (n % k != 0)
                {
                    if (n / k < k && n > k)
                    {
                        var division = k;
                        var i = 1;
                        while (k * (ulong)i < n)
                        {
                            division = k * (ulong)i;
                            i++;
                        }

                        totalPrice += (n - division) * a;
                        n = division;
                    }
                    else
                    {
                        ulong counter = 0;
                        while (n % k != 0 && n > 1)
                        {
                            counter++;
                            n--;
                        }

                        totalPrice += counter * a;
                    }
                }

                if (n == 1)
                    break;

                if (b > (n - n / k) * a) //გაყოფა უფრო ძვირია
                    totalPrice += (n - n / k) * a;

                else
                    totalPrice += b;

                n = n / k;

            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;

class Tanya{
	static void Main(){
		int n = Convert.ToInt32(Console.ReadLine());
		int k = Convert.ToInt32(Console.ReadLine());
		int A = Convert.ToInt32(Console.ReadLine());
		int B = Convert.ToInt32(Console.ReadLine());
		long mincoins = 0;
		int x = n;
		long costA;
		if (k == 1){
			mincoins = mincoins + (long) A*(x-1);
			Console.WriteLine(mincoins);
		}else if (n == 1){
			Console.WriteLine(mincoins);
		}else if (n > 1){
			while (x > 1){
				if (x % k == 0){
					costA = (long) A*(x - (x/k));
					if (costA < (long) B){
						if (x > k){
							x = x / k;
							mincoins = mincoins + costA;
						}else{
							x = x - (k-1);
							mincoins = mincoins + (long) A*(k-1);
						}
					}else{
						x = x / k;
						mincoins = mincoins + B;
					}
				}else{
					if ((x - x%k) > 0){
						mincoins = mincoins + (long) A*(x%k);
						x = x - (x%k);
					}else{
						mincoins = mincoins + (long) A*((x%k)-1);
						x = x - ((x%k)-1);
					}
				}
			}
			Console.WriteLine(mincoins);
		}
	}
}

using System;

namespace _940B
{
    public class Program
    {
        static void Main(string[] args)
        {
            var n = ulong.Parse(Console.ReadLine());
            var k = ulong.Parse(Console.ReadLine()); //გამყოფი
            var a = ulong.Parse(Console.ReadLine()); //გამოკლების ფასი
            var b = ulong.Parse(Console.ReadLine()); //გაყოფის ფასი
            ulong totalPrice = 0;

            if (k == 1 || n / k < 1)
            {
                Console.WriteLine((n - 1) * a);
                return;
            }

            while (n > 1)
            {
                if (n % k != 0)
                {
                    if (n / k < 100 && n > k)
                    {
                        var division = k;
                        var i = 1;
                        while (k * (ulong)i < n)
                        {
                            division = k * (ulong)i;
                            i++;
                        }

                        totalPrice += (n - division) * a;
                        n = division;
                    }
                    else
                    {
                        ulong counter = 0;
                        while (n % k != 0 && n > 1)
                        {
                            counter++;
                            n--;
                        }

                        totalPrice += counter * a;
                    }
                }

                if (n == 1)
                    break;

                if (b > (n - n / k) * a) //გაყოფა უფრო ძვირია
                    totalPrice += (n - n / k) * a;

                else
                    totalPrice += b;

                n = n / k;

            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;

namespace _940B
{
    public class Program
    {
        static void Main(string[] args)
        {
            var n = ulong.Parse(Console.ReadLine());
            var k = ulong.Parse(Console.ReadLine()); //გამყოფი
            var a = ulong.Parse(Console.ReadLine()); //გამოკლების ფასი
            var b = ulong.Parse(Console.ReadLine()); //გაყოფის ფასი
            ulong totalPrice = 0;

            if (k == 1 || n / k < 1)
            {
                Console.WriteLine((n - 1) * a);
                return;
            }

            while (n > 1)
            {
                if (n % k != 0)
                {
                    if (n / k < 10 && n > k)
                    {
                        var division = k;
                        var i = 1;
                        while (k * (ulong)i < n)
                        {
                            division = k * (ulong)i;
                            i++;
                        }

                        totalPrice += (n - division) * a;
                        n = division;
                    }
                    else
                    {
                        ulong counter = 0;
                        while (n % k != 0 && n > 1)
                        {
                            counter++;
                            n--;
                        }

                        totalPrice += counter * a;
                    }
                }

                if (n == 1)
                    break;

                if (b > (n - n / k) * a) //გაყოფა უფრო ძვირია
                    totalPrice += (n - n / k) * a;

                else
                    totalPrice += b;

                n = n / k;

            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tanja
{
    class Program
    {
        static void Main(string[] args)
        {
            long n = long.Parse(Console.ReadLine());
            long k = long.Parse(Console.ReadLine());
            long a = long.Parse(Console.ReadLine());
            long b = long.Parse(Console.ReadLine());

            long cost = 0;
            long t = k * a;
            if (n<k || k==1)
            {
                cost = (n - 1)*a;
            }
            else
            {
                while (n > k)
                {
                    long rem = n % k;
                    if (rem == 0)
                    {
                        cost += Math.Min(b, (n - n / k) * a);
                        n /= k;
                    }
                    else
                    {
                        cost += rem * a;
                        n -= rem;
                    }
                }

                if (n == k)
                    cost += Math.Min(b, (n - 1) * a);
                else
                    cost += (n - 1) * a;
            }

            Console.Write(cost);

        }
    }
}

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ulong n = ulong.Parse(Console.ReadLine());
            ulong k = ulong.Parse(Console.ReadLine());
            ulong a = ulong.Parse(Console.ReadLine());
            ulong b = ulong.Parse(Console.ReadLine());
            ulong sum = 0;
            if (k == 1)
            {
                Console.WriteLine(a * (n - 1));
                return;
            }
            while (n != 1)
            {
                if (n % k == 0)
                {
                    ulong v1 = a * (n - n / k), v2 = b;
                    sum += Math.Min(v1, v2);
                    n /= k;
                }
                else
                {
                    ulong n2 = n / k;
                    n2 = n2 * k;
                    if (n2 != 0)
                    {
                        sum += a * (n - n2);
                        n = n2;
                    }
                    else
                    {
                        sum += a * (n - 1);
                        break;
                    }
                }
            }
            Console.WriteLine(sum);
        }
    }
}

using System;
class Program
    {
        static void Main(string[] args)
        {
            int x, k, a, b;
            {
                string[] input = Console.In.ReadToEnd().Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                x = int.Parse(input[0]);
                k = int.Parse(input[1]);
                a = int.Parse(input[2]);
                b = int.Parse(input[3]);
            }
            int result = 0;
            for(int smllr; x>1; )
            {
                if((smllr=x%k)==0)
                {
                    if (((x - (smllr = x / k)) * a) <= b) { result = a * (x - 1); x = 1; break; }
                    else { x = smllr; result += b; }
                }
                else { result += smllr * a; x -= smllr; }
            }
            Console.WriteLine(x==0? result-a:result);
        }
    }

using System;
class Program
    {
        static void Main(string[] args)
        {
            long x, k, a, b;
            {
                string[] input = Console.In.ReadToEnd().Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                x = long.Parse(input[0]);
                k = long.Parse(input[1]);
                a = long.Parse(input[2]);
                b = long.Parse(input[3]);
            }
            long result = 0L;
            for(long smllr; x>1L; )
            {
                if((smllr=x%k)==0L)
                {
                    if (((x - (smllr = x / k)) * a) <= b) { result = a * (x - 1L); x = 1L; break; }
                    else { x = smllr; result += b; }
                }
                else { result += smllr * a; x -= smllr; }
            }
            Console.WriteLine(x==0L? result-a:result);
        }
    }

using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharp
{
    class _940B
    {
        public static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            int k = int.Parse(Console.ReadLine());
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());

            var queue = new Queue<int>(new int[] { n });
            var amount = new Dictionary<int, int> { { n, 0 } };

            while (queue.Any())
            {
                int x = queue.Dequeue();

                if (!amount.ContainsKey(1) || amount[x] + a * (x - 1) < amount[1])
                {
                    amount[1] = amount[x] + a * (x - 1);
                }

                int mod = x % k;
                int div = x / k;

                if (mod > 0)
                {
                    if (!amount.ContainsKey(x - mod) || amount[x] + a * mod < amount[x - mod])
                    {
                        queue.Enqueue(x - mod);
                        amount[x - mod] = amount[x] + a * mod;
                    }
                }
                else
                {
                    if (!amount.ContainsKey(div) || amount[x] + b < amount[div])
                    {
                        queue.Enqueue(div);
                        amount[div] = amount[x] + b;
                    }
                }
            }

            Console.WriteLine(amount[1]);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine()), k = Convert.ToInt32(Console.ReadLine()), a = Convert.ToInt32(Console.ReadLine()), b = Convert.ToInt32(Console.ReadLine());
            int ans = 0;
            while ( n!=1)
            {
                if ( n%k!=0 && n/k!=0)
                {
                    ans+= n % k * a;n -= n % k;
                }
                else if ( n<k)
                {
                    ans += ((n - 1) * a);
                    n = 1;
                }
                else
                {
                    if ( b <= a*(n-(n/k)))
                    {
                        ans += b;n/=k;
                    }
                    else
                    {
                        ans += a * (n - (n / k)); n /= k;
                    }
                }

            }
            Console.WriteLine(ans);
        }
    }
}

using System;

class Tanya{
	static void Main(){
		int n = Convert.ToInt32(Console.ReadLine());
		int k = Convert.ToInt32(Console.ReadLine());
		int A = Convert.ToInt32(Console.ReadLine());
		int B = Convert.ToInt32(Console.ReadLine());
		int mincoins = 0;
		int x = n;
		int costA;
		if (n == 1){
			Console.WriteLine(mincoins);
		}else if (n > 1){
			while (x > 1){
				if (x % k == 0){
					costA = A*(x - (x/k));
					if (costA < B){
						if ((x - k) > 0){
							mincoins = mincoins + (A*k);
							x = x - k;
						}else{
							x = x - 1;
							mincoins = mincoins + A;
						}
					}else{
						x = x / k;
						mincoins = mincoins + B;
					}
				}else{
					if ((x - x%k) > 0){
						mincoins = mincoins + A*(x%k);
						x = x - (x%k);
					}else{
						x = x - 1;
						mincoins = mincoins + A;
					}
				}
			}
			Console.WriteLine(mincoins);
		}
	}
}

using System;

class Tanya{
	static void Main(){
		int n = Convert.ToInt32(Console.ReadLine());
		int k = Convert.ToInt32(Console.ReadLine());
		int A = Convert.ToInt32(Console.ReadLine());
		int B = Convert.ToInt32(Console.ReadLine());
		long mincoins = 0;
		int x = n;
		int costA;
		if (k == 1){
			mincoins = mincoins + (long) A*(x-1);
			Console.WriteLine(mincoins);
		}else if (n == 1){
			Console.WriteLine(mincoins);
		}else if (n > 1){
			while (x > 1){
				if (x % k == 0){
					costA = A*(x - (x/k));
					if (costA < B){
						if (x > k){
							x = x / k;
							mincoins = mincoins + (long) costA;
						}else{
							x = x - (k-1);
							mincoins = mincoins + (long) A*(k-1);
						}
					}else{
						x = x / k;
						mincoins = mincoins + B;
					}
				}else{
					if ((x - x%k) > 0){
						mincoins = mincoins + (long) A*(x%k);
						x = x - (x%k);
					}else{
						mincoins = mincoins + (long) A*((x%k)-1);
						x = x - ((x%k)-1);
					}
				}
			}
			Console.WriteLine(mincoins);
		}
	}
}

using System;

namespace _940B
{
    public class Program
    {
        static void Main(string[] args)
        {
            var n = ulong.Parse(Console.ReadLine());
            var k = ulong.Parse(Console.ReadLine()); //გამყოფი
            var a = ulong.Parse(Console.ReadLine()); //გამოკლების ფასი
            var b = ulong.Parse(Console.ReadLine()); //გაყოფის ფასი
            ulong totalPrice = 0;

            if (k == 1 || n / k < 1)
            {
                Console.WriteLine((n - 1) * a);
                return;
            }

            while (n > 1)
            {
                if (n % k != 0)
                {
                    if (n / k < 100)
                    {
                        var division = k;
                        var i = 1;
                        while (division < n)
                        {
                            division = k * (ulong)i;
                            i++;
                        }

                        totalPrice += (n - division) * a;
                        n = division;
                    }
                    else
                    {
                        ulong counter = 0;
                        while (n % k != 0 && n > 1)
                        {
                            counter++;
                            n--;
                        }

                        totalPrice += counter * a;
                    }
                }

                if (n == 1)
                    break;

                if (b > (n - n / k) * a) //გაყოფა უფრო ძვირია
                    totalPrice += (n - n / k) * a;

                else
                    totalPrice += b;

                n = n / k;

            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;

namespace _940B
{
    public class Program
    {
        static void Main(string[] args)
        {
            var n = uint.Parse(Console.ReadLine());
            var k = uint.Parse(Console.ReadLine());
            var a = uint.Parse(Console.ReadLine());
            var b = uint.Parse(Console.ReadLine());
            ulong totalPrice = 0;

            if (k == 1)
            {
                Console.WriteLine((n - 1) * a);
                return;
            }

            var multipliers = new List<uint>();
            uint index = 1;
            while (true)
            {
                var current = k * index;
                if (current > n)
                    break;
                multipliers.Add(current);
                index++;
            }

            if (!multipliers.Any())
            {
                Console.WriteLine((n - 1) * a);
                return;
            }

            var last = multipliers.Last();
            totalPrice += (n - last) * a;
            n = last;

            for (int i = multipliers.Count - 1; i >= 0; --i)
            {
                if (n < multipliers[i])
                {
                    continue;
                }

                totalPrice += (n - multipliers[i]) * (ulong)a;

                uint divisionPrice = b;
                var minusPrice = (n - n / k) * (ulong)a;
                if (divisionPrice < minusPrice)
                {
                    totalPrice += divisionPrice;
                    n = n / k;
                }

                else
                {
                    totalPrice += (n - 1) * (ulong)a;
                    Console.WriteLine(totalPrice);
                    return;
                }
            }

            if (n > 1)
            {
                totalPrice += (n - 1) * a;
            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tanja
{
    class Program
    {
        static void Main(string[] args)
        {
            long n = long.Parse(Console.ReadLine());
            long k = long.Parse(Console.ReadLine());
            long a = long.Parse(Console.ReadLine());
            long b = long.Parse(Console.ReadLine());

            long cost = 0;
            if (b >= k * a || n<k || k==1)
            {
                cost = (n - 1)*a;
            }
            else
            {
                while (n > k)
                {
                    long rem = n % k;
                    if (rem == 0)
                    {
                        cost += b;
                        n /= k;
                    }
                    else
                    {
                        cost += rem * a;
                        n -= rem;
                    }
                }

                if (n == k)
                    cost += Math.Min(b, (n - 1) * a);
                else
                    cost += (n - 1) * a;
            }

            Console.Write(cost);

        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = int.Parse(Console.ReadLine());
            int div = int.Parse(Console.ReadLine());
            int subCost = int.Parse(Console.ReadLine());
            int divCost = int.Parse(Console.ReadLine());
            int cost = 0;
            while (x >= 1)
            {
                if (x == 1)
                {
                    cost += subCost;
                    break;
                }
                int rem = x % div;
                if (rem == 0)
                {
                    int sC = subCost * (x - x / div);
                    x /= div;
                    if (divCost < sC)
                    {
                        cost += divCost;
                    }
                    else
                    {
                        cost += sC;
                    }
                }
                else
                {
                    cost += subCost * rem;
                    x -= rem;
                }
            }
            cost -= subCost;
            Console.WriteLine(cost);
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = int.Parse(Console.ReadLine());
            int div = int.Parse(Console.ReadLine());
            int subCost = int.Parse(Console.ReadLine());
            int divCost = int.Parse(Console.ReadLine());
            int cost = 0;
            while (x >= 1)
            {
                if (x == 1)
                {
                    cost += subCost;
                    break;
                }
                int rem = x % div;
                Console.WriteLine("rem: " + rem + " x: " + x);
                if (rem == 0)
                {
                    int sC = subCost * (x - x / div);
                    x /= div;
                    if (divCost < sC)
                    {
                        cost += divCost;
                    }
                    else
                    {
                        cost += sC;
                    }
                }
                else
                {
                    cost += subCost * rem;
                    x -= rem;
                }
            }
            cost -= subCost;
            Console.WriteLine(cost);
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = int.Parse(Console.ReadLine());
            int div = int.Parse(Console.ReadLine());
            int subCost = int.Parse(Console.ReadLine());
            int divCost = int.Parse(Console.ReadLine());
            Console.WriteLine(subCost);
            Console.WriteLine(divCost);
            BigInteger cost = 0;
            if (div > 1)
            {
                while (x >= 1)
                {
                    if (x == 1)
                    {
                        cost += subCost;
                        break;
                    }
                    int rem = x % div;
                    Console.WriteLine("rem: " + rem + " x: " + x + " cost:" + cost);
                    if (rem == 0)
                    {
                        BigInteger sC = (BigInteger)subCost * (BigInteger)(x - x / div);
                        x /= div;
                        if (divCost < sC)
                        {
                            Console.WriteLine("dividing");
                            cost += divCost;
                        }
                        else
                        {
                            Console.WriteLine("subtracting");
                            cost += sC;
                        }
                    }
                    else
                    {
                        Console.WriteLine("subtracting " + subCost + "rem" + rem + subCost * rem);
                        cost += (BigInteger)subCost * (BigInteger)rem;
                        x -= rem;
                    }
                }
                cost -= subCost;
            }
            else
            {
                cost = (BigInteger)subCost * (BigInteger)(x - 1);
            }
            Console.WriteLine(cost);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace Quserty2
{
    public class Program
    {
        static ulong totalPrice = 0;

        static void Main(string[] args)
        {
            ulong n, k, A, B;

            n = ulong.Parse(Console.ReadLine());
            k = ulong.Parse(Console.ReadLine());
            A = ulong.Parse(Console.ReadLine());
            B = ulong.Parse(Console.ReadLine());

            if (k == 1)
            {
                Console.WriteLine((n - 1) * A);
                return;
            }

            ulong last = 0;

            uint index = 1;
            if (n / k < k)
            {
                while (true)
                {
                    var current = k * index;
                    if (current > n)
                        break;
                    last = current;
                    index++;
                }
            }
            else
            {
                var n2 = n;
                while (n2 % k != 0 && n2 > 1)
                {
                    --n2;
                }

                last = n2;
            }

            if (last == 0)
            {
                Console.WriteLine((n - 1) * A);
                return;
            }

            totalPrice += (n - last) * A;
            n = last;

            for (;;)
            {
                if (last <= 1)
                {
                    break;
                }

                if (n < last)
                {
                    last -= k;
                    continue;
                }

                totalPrice += (n - last) * A;

                var divisionPrice = B;
                var minusPrice = (n - n / k) * A;
                if (divisionPrice < minusPrice)
                {
                    totalPrice += divisionPrice;
                    var divisionResult = n / k;
                    var numberOfkTimesInDivisionResult = (n - divisionResult) / k;
                    last -= (numberOfkTimesInDivisionResult == 0 ? 1 : numberOfkTimesInDivisionResult) * k;
                    n = divisionResult;
                    continue;
                }
                else
                {
                    totalPrice += (n - 1) * A;
                    Console.WriteLine(totalPrice);
                    return;
                }
            }

            if (n > 1)
            {
                totalPrice += (n - 1) * A;
            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;
using System.Security.Principal;

namespace Quserty2
{
    public class Program
    {
        static void Main(string[] args)
        {
            uint n, k, A, B;

            n = uint.Parse(Console.ReadLine());
            k = uint.Parse(Console.ReadLine());
            A = uint.Parse(Console.ReadLine());
            B = uint.Parse(Console.ReadLine());

            ulong totalPrice = 0;

            while (n > 1)
            {
                if (n % k == 0)
                {
                    var divisionPrice = B;
                    var minusPrice = (n - n / k) * A;
                    if (divisionPrice < minusPrice)
                    {
                        totalPrice += divisionPrice;
                        n = n / k;
                    }
                    else
                    {
                        Console.WriteLine(totalPrice + n * A);
                        return;
                    }
                }
                else
                {
                    totalPrice += A;
                    n--;
                }
            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace Quserty2
{
    public class Program
    {
        static ulong totalPrice = 0;

        static void Main(string[] args)
        {
            ulong n, k, A, B;

            n = ulong.Parse(Console.ReadLine());
            k = ulong.Parse(Console.ReadLine());
            A = ulong.Parse(Console.ReadLine());
            B = ulong.Parse(Console.ReadLine());

            if (k == 1)
            {
                Console.WriteLine((n - 1) * A);
                return;
            }

            ulong last = 0;

            uint index = 1;
            if (n / k < k)
            {
                while (true)
                {
                    var current = k * index;
                    if (current > n)
                        break;
                    last = current;
                    index++;
                }
            }
            else
            {
                var n2 = n;
                while (n2 % k != 0 && n2 > 1)
                {
                    --n2;
                }

                last = n2;
            }

            if (last == 0)
            {
                Console.WriteLine((n - 1) * A);
                return;
            }

            totalPrice += (n - last) * A;
            n = last;

            for (;;)
            {
                if (last <= 1)
                {
                    break;
                }

                if (n < last)
                {
                    last -= k;
                    continue;
                }

                totalPrice += (n - last) * A;

                var divisionPrice = B;
                var minusPrice = (n - n / k) * A;
                if (divisionPrice < minusPrice)
                {
                    totalPrice += divisionPrice;
                    var divisionResult = n / k;
                    last -= (n - divisionResult) / k;
                    n = divisionResult;
                    continue;
                }
                else
                {
                    totalPrice += (n - 1) * A;
                    Console.WriteLine(totalPrice);
                    return;
                }
            }

            if (n > 1)
            {
                totalPrice += (n - 1) * A;
            }

            Console.WriteLine(totalPrice);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace Quserty2
{
    public class Program
    {
        static void Main(string[] args)
        {
            uint n, k, A, B;

            n = uint.Parse(Console.ReadLine());
            k = uint.Parse(Console.ReadLine());
            A = uint.Parse(Console.ReadLine());
            B = uint.Parse(Console.ReadLine());

            if (k == 1)
            {
                Console.WriteLine((n - 1) * A);
                return;
            }

            ulong totalPrice = 0;

            List<uint> multipliers = new List<uint>();

            uint index = 1;
            while (true)
            {
                var current = k * index;
                if(current > n)
                    break;
                multipliers.Add(current);
                index++;
            }

            var last = multipliers.Last();
            totalPrice += (n - last) * A;
            n = last;

            for (int i = multipliers.Count - 1; i >= 0; --i)
            {
                if (n < multipliers[i])
                {
                    continue;
                }

                totalPrice += (n - multipliers[i]) * A;

                var divisionPrice = B;
                var minusPrice = (n - n / k) * A;
                if (divisionPrice < minusPrice)
                {
                    totalPrice += divisionPrice;
                    n = n / k;
                }

                else
                {
                    Console.WriteLine(totalPrice + (n - 1) * A);
                    return;
                }
            }

            if (n > 1)
                totalPrice += (n - 1) * A;

            Console.WriteLine(totalPrice);
        }
    }
}

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var n = Convert.ToInt32(Console.ReadLine());
            var k = Convert.ToInt32(Console.ReadLine());
            var A = Convert.ToInt32(Console.ReadLine());
            var B = Convert.ToInt32(Console.ReadLine());

            var cost = 0;

            while(n > 1) {
                if(n%k == 0) {
                    if (B < (A * (n - n/k))) {
                        cost += B;
                        n /= k;
                    }
                    else {
                        cost += (A * (n - n/k));
                        n /= k;
                    }
                }
                else {
                    if(n - (n%k) == 0) {
                        cost += (A * (n%k - 1));
                        n -= (n%k + 1);
                    }
                    else {
                        cost += (A * (n%k));
                        n -= (n%k);
                    }
                }
            }

            Console.WriteLine(cost);
        }
    }
}

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            UInt64 n = Convert.ToUInt64(Console.ReadLine());
            UInt64 k = Convert.ToUInt64(Console.ReadLine());
            UInt64 A = Convert.ToUInt64(Console.ReadLine());
            UInt64 B = Convert.ToUInt64(Console.ReadLine());

            UInt64 cost = 0;

            while(n > 1) {
                if(n%k == 0) {
                    if (B < (A * (n - n/k))) {
                        cost += B;
                        n /= k;
                    }
                    else {
                        cost += (A * (n - n/k));
                        n /= k;
                    }
                }
                else {
                    if(n - (n%k) == 0) {
                        cost += (A * (n%k - 1));
                        n -= (n%k + 1);
                    }
                    else {
                        cost += (A * (n%k));
                        n -= (n%k);
                    }
                }
            }

            Console.WriteLine(cost);
        }
    }
}