#include <bits/stdc++.h>
using namespace std;
ifstream in("koba.in");
ofstream out("koba.out");
const int DIM = 1e3 + 7;
int viz[DIM];
vector <int> sp;
int main()
{
int n, x, y , z;
in >> n >> x >> y >> z;
x = x % 10;
y = y % 10;
z = z % 10;
if(n <= 3)
{
if(n == 1)
out << x;
else
if(n == 2)
out << y;
else
if(n = 3)
out << z;
return 0;
}
viz[x * 100 + y * 10 + z] = 3;
n-= 3;
int rez = x + y + z;
sp.push_back(x);
sp.push_back(sp[sp.size() - 1] + y);
sp.push_back(sp[sp.size() - 1] + z);
for(int i = 1; i <= n; i++)
{
int q = (z + (y * x) % 10) % 10;
x = y;
y = z;
z = q;
rez += q;
sp.push_back(sp[sp.size() - 1] + q);
if(viz[x * 100 + y * 10 + z] == 0)
{
viz[x * 100 + y * 10 + z] = i + 3;
}
else
{
rez = rez - z - y - x;
int p = sp[sp.size() - 4] - sp[viz[x * 100 + y * 10 + z] - 4];
int lp = sp.size() - 4 - viz[x * 100 + y * 10 + z] + 4;
rez += (n - i + 3) / lp * p;
int r = (n - i + 3) % lp;
rez += sp[viz[x * 100 + y * 10 + z] - 4 + r] - sp[viz[x * 100 + y * 10 + z] - 4];
out << rez;
return 0;
}
}
out << rez;
return 0;
}