﻿namespace _02.TextToNumber
{
    using System;
    using System.Threading;
    using System.Globalization;

    class TextToNumber
    {
        static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            double m = double.Parse(Console.ReadLine());
            string input = Console.ReadLine();

            input = input.ToLower();
            double result = 0;

            foreach (char ch in input)
            {
                if (ch == '@')
                {
                    Console.WriteLine(result);
                    break;
                }
                else if (Char.IsDigit(ch))
                {
                    result *= Char.GetNumericValue(ch);
                }
                else if (ch >= 'a' && ch <= 'z')
                {
                    result += ch - 97;
                }
                else
                {
                    result %= m;
                }
            }

        }
    }
}
