#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <unordered_map>

using namespace std;
using ll = long long;

void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); }
void _cout(){ cout << "\n"; } template <class Head, class... Tail> void _cout(Head&& head, Tail&&... tail){ cout << head; _cout(forward<Tail>(tail)...); }
int gcd(int a, int b){ return (b == 0) ? a : gcd(b, a % b); }

#define Sq(x) (x)*(x)
#define For(i, n) for(int i = 0; i < (n); i ++)
#define Rep(n) For(_, n)
#define Range(c) c.begin(), c.end()
#define RevRange(c) c.rbegin(), c.rend()
#define Contains(c, x) (find(Range(c), x) != c.end())
#define Search(rb, re, x) distance(rb, find(rb, re, x))
#define Sort(a) sort(Range(a))
#define DeSort(a) sort(RevRange(a))
#define Reverse(c) reverse(Range(c))
#define Unique(a) a.erase(unique(Range(a)), a.end())
#define Vec2(T, n, m, xs) vector<vector<T>> xs(n, vector<T>(m))
#define Sum(a) accumulate(Range(a), 0)
#define Cusum(T, xs, sxs) vector<T> sxs(xs.size()+1); For(i, (int)xs.size()) sxs[i+1] = sxs[i] + xs[i]
#define Cin(T, ...) T __VA_ARGS__; _cin(__VA_ARGS__)
#define Cins(T, n, xs) vector<T> xs(n); For(i, n) cin >> xs[i]
#define Cins2(T, n, xs, ys) vector<T> xs(n), ys(n); For(i, n) cin >> xs[i] >> ys[i]
#define Cins3(T, n, xs, ys, zs) vector<T> xs(n), ys(n), zs(n); For(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinss(T, n, m, xs) Vec2(T, n, m, xs); For(i, n) For(j, m) cin >> xs[i][j]
#define Cinm(T, n, map) unordered_map<T, int> map; Rep(n){ Cin(T, x); map[x] ++; }
#define Cout(...) _cout(__VA_ARGS__)
#define Couts(xs) for(const auto &e : xs) cout << e << " "; cout << "\n"
#define Coutyn(cond) Cout((cond) ? "yes" : "no")
#define CoutYn(cond) Cout((cond) ? "Yes" : "No")
#define CoutYN(cond) Cout((cond) ? "YES" : "NO")

int MOD;

struct M3{
    ll x1, x2, x3, y1, y2, y3, z1, z2, z3;
    M3(ll x1, ll x2, ll x3, ll y1, ll y2, ll y3, ll z1, ll z2, ll z3)
    : x1(x1 % MOD), x2(x2 % MOD), x3(x3 % MOD),
    y1(y1 % MOD), y2(y2 % MOD), y3(y3 % MOD),
    z1(z1 % MOD), z2(z2 % MOD), z3(z3 % MOD){}
    
    void print() const{
        Cout(x1, " ", x2, " ", x3);
        Cout(y1, " ", y2, " ", y3);
        Cout(z1, " ", z2, " ", z3);
    }
};

M3 operator*(const M3 &a, const M3 &b){
    return M3(
        (a.x1*b.x1 % MOD + a.x2*b.y1 % MOD + a.x3*b.z1 % MOD) % MOD,
        (a.x1*b.x2 % MOD + a.x2*b.y2 % MOD + a.x3*b.z2 % MOD) % MOD,
        (a.x1*b.x3 % MOD + a.x2*b.y3 % MOD + a.x3*b.z3 % MOD) % MOD,
        (a.y1*b.x1 % MOD + a.y2*b.y1 % MOD + a.y3*b.z1 % MOD) % MOD,
        (a.y1*b.x2 % MOD + a.y2*b.y2 % MOD + a.y3*b.z2 % MOD) % MOD,
        (a.y1*b.x3 % MOD + a.y2*b.y3 % MOD + a.y3*b.z3 % MOD) % MOD,
        (a.z1*b.x1 % MOD + a.z2*b.y1 % MOD + a.z3*b.z1 % MOD) % MOD,
        (a.z1*b.x2 % MOD + a.z2*b.y2 % MOD + a.z3*b.z2 % MOD) % MOD,
        (a.z1*b.x3 % MOD + a.z2*b.y3 % MOD + a.z3*b.z3 % MOD) % MOD
    );
}

M3 operator^(const M3 &a, ll n){
    if(n == 0) return M3(1, 0, 0, 0, 1, 0, 0, 0, 1);
    M3 m = a ^ (n/2);
    if(n % 2 == 0) return m * m;
    return m * m * a;
}

int main(void){
    Cin(ll, l, a, b, m);
    MOD = m;
    
    ll p10[20] = {1};
    For(i, 20) p10[i+1] = p10[i] * 10;
    vector<ll> di(20); di[0] = -1;
    
    for(int d = 1; d < 20; d ++){
        ll lb = -1, ub = l;
        while(ub - lb > 1){
            ll m = (lb + ub) / 2;
            ((a + b * m >= p10[d-1]) ? ub : lb) = m;
        }
        di[d] = ub;
    }
    // Couts(di);
    ll ans = 0;
    for(int d = 1; d <= 18; d ++){
        M3 m(p10[d], 1, 0, 0, 1, 1, 0, 0, 1);
        ll n = di[d+1] - di[d];
        M3 c = m ^ n;
        // m.print();
        // Cout("^ ", n, " =");
        // c.print();
        tie(ans, a, b) = make_tuple(
            (c.x1*ans + c.x2*a + c.x3*b) % MOD,
            (c.y1*ans + c.y2*a + c.y3*b) % MOD,
            (c.z1*ans + c.z2*a + c.z3*b) % MOD
        );
        // Cout(">>> ", ans, " ", a, " ", b);
    }
    Cout(ans);
}


Measured memory_bytes: 256