[TASK]:
You will be given lines of pseudocode, your task is to write the corresponding C++ code. The pseudocode will provide detailed description of the c++ code line by line. The pseudocode is garanteed to be correct and complete.

[INSTRUCTION]:
The following libraries are already included in the code.
```cpp
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <fstream>
#include <climits>
#include <cassert>
#include <iomanip>
#include <sstream>
#include <bitset>
using namespace std;
```
Do not include them in your code again. Please surround your code with ```cpp and ``` markers. Note that the code should correspond to the pseudocode line by line.

[PSEUDOCODE]:
vis1 = array of long integers of length 1000005
vis2 = array of long integers of length 1000005
define the main function
    let m be a long integer
    let h1, a1 be long integers
    let x1, y1 be long integers
    let h2, a2 be long integers
    let x2, y2 be long integers
    read m, h1, a1, x1, y1, h2, a2, x2, y2
    memset of vis1, 0, size of vis1
    memset of vis2, 0, size of vis2
    let t1, t2 be long integers with t1 = -1, t2 = -1
    let c1, c2 be long integers with c1 = -1, c2 = -1
    let q1, q2 be long integers with q1 = -1, q2 = -1
    let f1, f2 be long integers with f1 = -1, f2 = -1
    set boolean value flag to true
    set boolean value fuck to false
    for long integer i = 1 to 2 * m inclusive
        the value of h1 = h1 * x1
        the value of h1 = h1 modulo m
        the value of h1 = h1 + y1
        the value of h1 = h1 modulo m
        the value of h2 = h2 * x2
        the value of h2 = h2 modulo m
        the value of h2 = h2 + y2
        the value of h2 = h2 modulo m
        if h1 is equal to a1 and a2 is equal to h2
            set fuck to true
            print i and newline
            stop
        end of the block
        if vis1[a1] is equal to 0 and h1 is equal to a1, f1 is equal to i
        if vis2[a2] is equal to 0 and h2 is equal to a2, f2 is equal to i
        if h1 is equal to a1 and t1 is equal to -1, c1 is equal to i
        if h2 is equal to a2 and t2 is equal to -1, c2 is equal to i
        if vis1[h1] is not equal to 0 and q1 is equal to -1
            the value of q1 is equal to vis1[h1]
            the value of t1 is equal to i - q1
            if c1 is less than q1
                set flag to false
                stop
            else do the following
                decrement c1 by q1
            end of the block
        end of the block
        if vis2[h2] is not equal to 0 and q2 is equal to -1
            the value of q2 is equal to vis2[h2]
            the value of t2 is equal to i - q2
            if c2 is less than q2
                set flag to false
                stop
            else do the following
                decrement c2 by q2
            end of the block
        end of the block
        if t1 is not equal to -1 and t2 is not equal to -1, stop
        if vis1[h1] is equal to 0, vis1[h1] is equal to i
        if vis2[h2] is equal to 0, vis2[h2] is equal to i
    end of the block
    if fuck is true
    else if not flag
        if f1 is not equal to f2 or f1 is equal to -1 or f2 is equal to -1
            print -1 and newline
        else do the following
            print f1 + 1 and newline
    else do the following
        if f1 is equal to f2 and f1 is not equal to -1 and f2 is not equal to -2
            print f1 and newline
        else if t1 is equal to t2 and (c1 is equal to c2 and f1 is not equal to f2) or c1 is not equal to c2
            print -1 and newline
        end of the block
        else do the following
            let x be a long integer with x = 0
            while x * t1 + c1 + q1 - c2 - q2 modulo t2 is not equal to 0
                increment x by 1
                if x is greater than 1000000, stop
            end of the block
            if x is greater than 1000000
                print -1 and newline
            else do the following
                print x * t1 + c1 + q1 and newline
        end of the block
    end of the block
    return to indicate successful execution
end of the block

[CODE]:

long long vis1[1000005];
long long vis2[1000005];
int main() {
    long long m;
    long long h1, a1;
    long long x1, y1;
    long long h2, a2;
    long long x2, y2;
    cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
    memset(vis1, 0, sizeof(vis1));
    memset(vis2, 0, sizeof(vis2));
    long long t1 = -1, t2 = -1;
    long long c1 = -1, c2 = -1;
    long long q1 = -1, q2 = -1;
    long long f1 = -1, f2 = -1;
    bool flag = true;
    bool fuck = false;
    for (long long i = 1; i <= 2 * m; i++) {
        h1 *= x1;
        h1 %= m;
        h1 += y1;
        h1 %= m;
        h2 *= x2;
        h2 %= m;
        h2 += y2;
        h2 %= m;
        if (h1 == a1 && a2 == h2) {
            fuck = true;
            cout << i << endl;
            break;
        }
        if (vis1[a1] == 0 && h1 == a1) { f1 = i; }
        if (vis2[a2] == 0 && h2 == a2) { f2 = i; }
        if (h1 == a1 && t1 == -1) { c1 = i; }
        if (h2 == a2 && t2 == -1) { c2 = i; }
        if (vis1[h1] != 0 && q1 == -1) {
            q1 = vis1[h1];
            t1 = i - q1;
            if (c1 < q1) {
                flag = false;
                break;
            } else {
                c1 = c1 - q1;
            }
        }
        if (vis2[h2] != 0 && q2 == -1) {
            q2 = vis2[h2];
            t2 = i - q2;
            if (c2 < q2) {
                flag = false;
                break;
            } else {
                c2 = c2 - q2;
            }
        }
        if (t1 != -1 && t2 != -1) { break; }
        if (vis1[h1] == 0) vis1[h1] = i;
        if (vis2[h2] == 0) vis2[h2] = i;
    }
    if (fuck) {
    } else if (!flag) {
        if (f1 != f2 || f1 == -1 || f2 == -1)
            cout << "-1\n";
        else
            cout << f1 + 1 << "\n";
    } else {
        if (f1 == f2 && f1 != -1 && f2 != -2) {
            cout << f1 << "\n";
        } else if (t1 == t2 && ((c1 == c2 && f1 != f2) || c1 != c2)) {
            cout << "-1\n";
        }
        else {
            long long x = 0;
            while ((x * t1 + c1 + q1 - c2 - q2) % t2 != 0) {
                x++;
                if (x > 1000000) { break; }
            }
            if (x > 1000000) {
                cout << "-1" << endl;
            } else
                cout << x * t1 + c1 + q1 << "\n";
        }
    }
    return 0;
}