#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll grid[1009][1009];
#define mod 1000000007
vector<vector<ll> > v(2,vector<ll>(2,1));
ll fibo(ll n)
{
vector<vector<ll> > x(2,vector<ll>(2,1));
vector<vector<ll> > y(2,vector<ll>(2,1));
vector<vector<ll> > z(2,vector<ll>(2,0)); //temporary
ll i,j,k;
y[0][0]=v[0][0];
y[0][1]=v[0][1];
//y[0][2]=v[0][2];
y[1][0]=v[1][0];
y[1][1]=v[1][1];
//y[1][2]=v[1][2];
//y[2][0]=v[2][0];
//y[2][1]=v[2][1];
//y[2][2]=v[2][2];
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
x[i][j]=1;
}
}
while(n>0)
{
if(n&1) //x=x*y
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
z[i][j]=0;
for(k=0;k<2;k++)
{
z[i][j]=((z[i][j])%mod+(((x[i][k])%mod)*((y[k][j])%mod)%mod))%mod;
}
}
}
x[0][0]=z[0][0];
x[0][1]=z[0][1];
//x[0][2]=z[0][2];
x[1][0]=z[1][0];
x[1][1]=z[1][1];
//x[1][2]=z[1][2];
//x[2][0]=z[2][0];
//x[2][1]=z[2][1];
//x[2][2]=z[2][2];
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
z[i][j]=0;
for(k=0;k<2;k++)
{
z[i][j]=((z[i][j])%mod+(((y[i][k])%mod)*((y[k][j])%mod))%mod)%mod;
}
}
}
y[0][0]=z[0][0];
y[0][1]=z[0][1];
//y[0][2]=z[0][2];
y[1][0]=z[1][0];
y[1][1]=z[1][1];
//y[1][2]=z[1][2];
//y[2][0]=z[2][0];
//y[2][1]=z[2][1];
//y[2][2]=z[2][2];
n=n/2;
}
return x[0][0];
}
ll res(ll x)
{
if(x==1)
{
return 1;
}
return fibo(x-2);
}
ll *id, cnt, *sz;
void init(ll N)
{
cnt = N;
id = new ll[N+1];
sz = new ll[N+1];
for(ll i=1; i<=N; i++)
{
id[i] = i;
sz[i] = 1;
}
}
ll find(ll p)
{
ll root = p;
while (root != id[root])
root = id[root];
while (p != root) {
ll newp = id[p];
id[p] = root;
p = newp;
}
return root;
}
// Replace sets containing x and y with their union.
void merge(ll x, ll y)
{
ll i = find(x);
ll j = find(y);
if (i == j) return;
// make smaller root point to larger one
if(sz[i]<sz[j])
{
id[i]=j;
sz[j]+=sz[i];
}
else
{
id[j]=i;
sz[i]+=sz[j];
}
cnt--;
}
// Are objects x and y in the same set?
bool connected(ll x, ll y)
{
return find(x) == find(y);
}
// Return the number of disjoint sets.
ll count()
{
return cnt;
}
void destroy()
{
delete []id;
delete []sz;
}
//////////////////////////UFDS ends.
//////////////////////////MST code begins
const ll MAX = 1000099;
pair <long long, pair<ll, ll> > p[MAX];
ll nodes,edges;
long long kruskal()
{
ll x, y;
long long cost, minimumCost = 0;
for(ll i = 1;i < edges;++i)
{
x = p[i].second.first;
y = p[i].second.second;
cost = p[i].first;
if(find(x) != find(y))
{
minimumCost += cost;
merge(x, y);
}
}
return minimumCost;
}
ll n;
ll mp(ll x,ll y)
{
return n*(x-1)+y;
}
ll x,y,wt;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll minimumCost,t,m,i,j,k1,k2,k3,k4,temp1,temp2;
v[0][0]=1;
v[0][1]=1;
v[1][0]=1;
v[1][1]=0;
cin>>n>>k1>>k2>>k3>>k4;
init(n*n+1);
ll num=1;
ll ans1=res(k1)%mod;
ll ans2=res(k2)%mod;
//cout<<ans1<<" "<<ans2<<"\n";
ll next_ans1=res(k1+1)%mod;
ll next_ans2=res(k2+1)%mod;
//cout<<next_ans1<<" "<<next_ans2<<"\n";
for(i=1;i<=n;i++)
{
for(j=1;j<n;j++)
{
temp1=mp(i,j);
temp2=mp(i,j+1);
x=temp1;
y=temp2;
wt=(ans1%mod+ans2%mod)%mod;
p[num] = make_pair(wt, make_pair(x, y));
//cout<<wt[num]<<"\n";
ll z1=(ans1%mod+next_ans1%mod)%mod;
ll z2=(ans2%mod+next_ans2%mod)%mod;
ans1=next_ans1%mod;
ans2=next_ans2%mod;
next_ans1=z1%mod;
next_ans2=z2%mod;
//cout<<ans1<<" "<<ans2<<"\n";
num++;
}
}
ans1=res(k3)%mod;
ans2=res(k4)%mod;
//cout<<ans1<<" "<<ans2<<"\n";
next_ans1=res(k3+1)%mod;
next_ans2=res(k4+1)%mod;
for(i=1;i<=n;i++)
{
for(j=1;j<n;j++)
{
temp1=mp(j,i);
temp2=mp(j+1,i);
x=temp1;
y=temp2;
wt=(ans1%mod+ans2%mod)%mod;
p[num] = make_pair(wt, make_pair(x, y));
//cout<<wt[num]<<"\n";
ll z1=(ans1%mod+next_ans1%mod)%mod;
ll z2=(ans2%mod+next_ans2%mod)%mod;
ans1=next_ans1%mod;
ans2=next_ans2%mod;
next_ans1=z1%mod;
next_ans2=z2%mod;
num++;
}
}
edges=num;
sort(p, p + edges);
minimumCost = kruskal();
cout << minimumCost << endl;
destroy();
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKbGwgZ3JpZFsxMDA5XVsxMDA5XTsKI2RlZmluZSBtb2QgMTAwMDAwMDAwNwp2ZWN0b3I8dmVjdG9yPGxsPiA+IHYoMix2ZWN0b3I8bGw+KDIsMSkpOwpsbCBmaWJvKGxsIG4pCnsKICAgIHZlY3Rvcjx2ZWN0b3I8bGw+ID4geCgyLHZlY3RvcjxsbD4oMiwxKSk7CiAgICB2ZWN0b3I8dmVjdG9yPGxsPiA+IHkoMix2ZWN0b3I8bGw+KDIsMSkpOwogICAgdmVjdG9yPHZlY3RvcjxsbD4gPiB6KDIsdmVjdG9yPGxsPigyLDApKTsgICAvL3RlbXBvcmFyeQogICAgbGwgaSxqLGs7CiAgICB5WzBdWzBdPXZbMF1bMF07CiAgICB5WzBdWzFdPXZbMF1bMV07CiAgICAvL3lbMF1bMl09dlswXVsyXTsKICAgIHlbMV1bMF09dlsxXVswXTsKICAgIHlbMV1bMV09dlsxXVsxXTsKICAgIC8veVsxXVsyXT12WzFdWzJdOwogICAgLy95WzJdWzBdPXZbMl1bMF07CiAgICAvL3lbMl1bMV09dlsyXVsxXTsKICAgIC8veVsyXVsyXT12WzJdWzJdOwogICAgZm9yKGk9MDtpPDI7aSsrKQogICAgewogICAgICAgIGZvcihqPTA7ajwyO2orKykKICAgICAgICB7CiAgICAgICAgICAgIHhbaV1bal09MTsKICAgICAgICB9CiAgICB9CiAgICB3aGlsZShuPjApCiAgICB7CiAgICAgICAgaWYobiYxKSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy94PXgqeQogICAgICAgIHsKICAgICAgICAgICAgZm9yKGk9MDtpPDI7aSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3Ioaj0wO2o8MjtqKyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgeltpXVtqXT0wOwogICAgICAgICAgICAgICAgICAgIGZvcihrPTA7azwyO2srKykKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHpbaV1bal09KCh6W2ldW2pdKSVtb2QrKCgoeFtpXVtrXSklbW9kKSooKHlba11bal0pJW1vZCklbW9kKSklbW9kOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICB4WzBdWzBdPXpbMF1bMF07CiAgICAgICAgICAgIHhbMF1bMV09elswXVsxXTsKICAgICAgICAgICAgLy94WzBdWzJdPXpbMF1bMl07CiAgICAgICAgICAgIHhbMV1bMF09elsxXVswXTsKICAgICAgICAgICAgeFsxXVsxXT16WzFdWzFdOwogICAgICAgICAgICAvL3hbMV1bMl09elsxXVsyXTsKICAgICAgICAgICAgLy94WzJdWzBdPXpbMl1bMF07CiAgICAgICAgICAgIC8veFsyXVsxXT16WzJdWzFdOwogICAgICAgICAgICAvL3hbMl1bMl09elsyXVsyXTsKICAgICAgICB9CiAgICAgICAgICAgIGZvcihpPTA7aTwyO2krKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yKGo9MDtqPDI7aisrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHpbaV1bal09MDsKICAgICAgICAgICAgICAgICAgICBmb3Ioaz0wO2s8MjtrKyspCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICB6W2ldW2pdPSgoeltpXVtqXSklbW9kKygoKHlbaV1ba10pJW1vZCkqKCh5W2tdW2pdKSVtb2QpKSVtb2QpJW1vZDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgeVswXVswXT16WzBdWzBdOwogICAgICAgICAgICB5WzBdWzFdPXpbMF1bMV07CiAgICAgICAgICAgIC8veVswXVsyXT16WzBdWzJdOwogICAgICAgICAgICB5WzFdWzBdPXpbMV1bMF07CiAgICAgICAgICAgIHlbMV1bMV09elsxXVsxXTsKICAgICAgICAgICAgLy95WzFdWzJdPXpbMV1bMl07CiAgICAgICAgICAgIC8veVsyXVswXT16WzJdWzBdOwogICAgICAgICAgICAvL3lbMl1bMV09elsyXVsxXTsKICAgICAgICAgICAgLy95WzJdWzJdPXpbMl1bMl07CiAgICAgICAgICAgIG49bi8yOwogICAgfQogICAgcmV0dXJuIHhbMF1bMF07Cn0KbGwgcmVzKGxsIHgpCnsKICAgIGlmKHg9PTEpCiAgICB7CiAgICAgICAgcmV0dXJuIDE7CiAgICB9CiAgICByZXR1cm4gZmlibyh4LTIpOwp9CmxsICppZCwgY250LCAqc3o7CnZvaWQgaW5pdChsbCBOKQp7CiAgICBjbnQgPSBOOwoJaWQgPSBuZXcgbGxbTisxXTsKCXN6ID0gbmV3IGxsW04rMV07CiAgICBmb3IobGwgaT0xOyBpPD1OOyBpKyspCiAgICB7CiAgICAgICAgaWRbaV0gPSBpOwoJICAgIHN6W2ldID0gMT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AgIGxsIG5leHRfYW5zMT1yZXMoazErMSklbW9kOwogICAgbGwgbmV4dF9hbnMyPXJlcyhrMisxKSVtb2Q7CiAgICAvL2NvdXQ8PG5leHRfYW5zMTw8IiAiPDxuZXh0X2FuczI8PCJcbiI7CiAgICBmb3IoaT0xO2k8PW47aSsrKQogICAgewogICAgICAgIGZvcihqPTE7ajxuO2orKykKICAgICAgICB7CiAgICAgICAgICAgIHRlbXAxPW1wKGksaik7CiAgICAgICAgICAgIHRlbXAyPW1wKGksaisxKTsKICAgICAgICAgICAgeD10ZW1wMTsKICAgICAgICAgICAgeT10ZW1wMjsKICAgICAgICAgICAgd3Q9KGFuczElbW9kK2FuczIlbW9kKSVtb2Q7CiAgICAgICAgICAgIHBbbnVtXSA9IG1ha2VfcGFpcih3dCwgbWFrZV9wYWlyKHgsIHkpKTsKICAgICAgICAgICAgLy9jb3V0PDx3dFtudW1dPDwiXG4iOwogICAgICAgICAgICBsbCB6MT0oYW5zMSVtb2QrbmV4dF9hbnMxJW1vZCklbW9kOwogICAgICAgICAgICBsbCB6Mj0oYW5zMiVtb2QrbmV4dF9hbnMyJW1vZCklbW9kOwogICAgICAgICAgICBhbnMxPW5leHRfYW5zMSVtb2Q7CiAgICAgICAgICAgIGFuczI9bmV4dF9hbnMyJW1vZDsKICAgICAgICAgICAgbmV4dF9hbnMxPXoxJW1vZDsKICAgICAgICAgICAgbmV4dF9hbnMyPXoyJW1vZDsKICAgICAgICAgICAgLy9jb3V0PDxhbnMxPDwiICI8PGFuczI8PCJcbiI7CiAgICAgICAgICAgIG51bSsrOwogICAgICAgIH0KICAgIH0KICAgIGFuczE9cmVzKGszKSVtb2Q7CiAgICBhbnMyPXJlcyhrNCklbW9kOwogICAgLy9jb3V0PDxhbnMxPDwiICI8PGFuczI8PCJcbiI7CiAgICBuZXh0X2FuczE9cmVzKGszKzEpJW1vZDsKICAgIG5leHRfYW5zMj1yZXMoazQrMSklbW9kOwogICAgZm9yKGk9MTtpPD1uO2krKykKICAgIHsKICAgICAgICBmb3Ioaj0xO2o8bjtqKyspCiAgICAgICAgewogICAgICAgICAgICB0ZW1wMT1tcChqLGkpOwogICAgICAgICAgICB0ZW1wMj1tcChqKzEsaSk7CiAgICAgICAgICAgIHg9dGVtcDE7CiAgICAgICAgICAgIHk9dGVtcDI7CiAgICAgICAgICAgIHd0PShhbnMxJW1vZCthbnMyJW1vZCklbW9kOwogICAgICAgICAgICBwW251bV0gPSBtYWtlX3BhaXIod3QsIG1ha2VfcGFpcih4LCB5KSk7CiAgICAgICAgICAgIC8vY291dDw8d3RbbnVtXTw8IlxuIjsKICAgICAgICAgICAgbGwgejE9KGFuczElbW9kK25leHRfYW5zMSVtb2QpJW1vZDsKICAgICAgICAgICAgbGwgejI9KGFuczIlbW9kK25leHRfYW5zMiVtb2QpJW1vZDsKICAgICAgICAgICAgYW5zMT1uZXh0X2FuczElbW9kOwogICAgICAgICAgICBhbnMyPW5leHRfYW5zMiVtb2Q7CiAgICAgICAgICAgIG5leHRfYW5zMT16MSVtb2Q7CiAgICAgICAgICAgIG5leHRfYW5zMj16MiVtb2Q7CiAgICAgICAgICAgIG51bSsrOwogICAgICAgIH0KICAgIH0KICAgIGVkZ2VzPW51bTsKICAgIHNvcnQocCwgcCArIGVkZ2VzKTsKICAgIG1pbmltdW1Db3N0ID0ga3J1c2thbCgpOwogICAgY291dCA8PCBtaW5pbXVtQ29zdCA8PCBlbmRsOwogICAgZGVzdHJveSgpOwogICAgcmV0dXJuIDA7Cn0K