#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 600010;
int _w;
int n, m, a[N], vis[N];
void input() {
ll x, a, b, c;
ll y, u, v, w;
cin >> x >> a >> b >> c;
cin >> y >> u >> v >> w;
for( int i = 1; i <= n; ++i )
::a[i] = i;
while( m-- ) {
x = (a*x*x + b*x + c) % n + 1;
y = (u*y*y + v*y + w) % n + 1;
swap( ::a[x], ::a[y] );
}
}
int main() {
freopen( "tui_problem.in", "r", stdin );
freopen( "tui_problem.out", "w", stdout );
cin >> n >> m;
input();
int ans = 0;
for( int i = 1; i <= n; ++i ) {
for( int j = 1; j <= n; ++j )
vis[j] = 0;
int cnt = 0;
for( int j = i; j <= n; ++j ) {
int x = a[j];
vis[x] = 1;
if( vis[x-1] ) --cnt;
if( vis[x+1] ) --cnt;
if( j-i+1+cnt <= 2 ) ++ans;
}
}
cout << ans-n << endl;
return 0;
}