[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]:
create integers m, h, h2, a, a2, x, x2, y, y2
declare get with integers s, e as arguments, returning integer
    for i = 0 to m exclusive
        set s to (1ll * s * x + y ) % m
        if s is e, return i + 1
    end of the block
    return -1 from function
end of the block
declare get2 with integers s, e as arguments, returning integer
    for i = 0 to m exclusive
        set s to (1ll * s * x2 + y2 ) % m
        if s is e, return i + 1 from function
    end of the block
    return -1 from function
end of the block
define the main function
    read m read h read a read x read y read h2 read a2 read x2 read y2
    create integers t, t2 with t = result of run get with h, a as arguments, t2 = result of run get2 with h2, a2 as arguments
    if t is -1 or t2 is -1, return print "-1\n", 0 from function
    if t is t2, return print t print '\n', 0 from function
    create integers l, l2 with l = result of run get with a, a as arguments, l2 = result of run get2 with a2, a2, as arguments
    if bitwise not l and t - t2 is greater than or equal to 0 and not ( (t-t2)%l2), return print t print '\n', 0 from function
    if bitwise not l2 and t - t2 is greater than or equal to 0 and not ( (t-t2) % l2 ), return print t print '\n', 0 from function
    if l is -1 or l2 is -1, return print "-1\n", 0
    create integer val with val = t2 - t
    for i =1, lim = m bitshift left 1 to i is less than or equal to lim, incrementing i
        if (val + 1ll * i * l2 is greater than or equal to 0 and not ((val + 1ll * i * l2) % l)) return print 1ll * i * l2 + t2 print '\n', 0 from function
    print "-1\n"
    return to indicate successful execution
end of the block

[CODE]:

int m, h, h2, a, a2, x, x2, y, y2;
int get(int s, int e) {
    for (int i = 0; i < m; ++i) {
        s = (1ll * s * x + y) % m;
        if (s == e) return i + 1;
    }
    return -1;
}
int get2(int s, int e) {
    for (int i = 0; i < m; ++i) {
        s = (1ll * s * x2 + y2) % m;
        if (s == e) return i + 1;
    }
    return -1;
}
int main() {
    cin >> m >> h >> a >> x >> y >> h2 >> a2 >> x2 >> y2;
    int t = get(h, a), t2 = get2(h2, a2);
    if (t == -1 || t2 == -1) return cout << "-1\n", 0;
    if (t == t2) return cout << t << '\n', 0;
    int l = get(a, a), l2 = get2(a2, a2);
    if (~l && t2 - t >= 0 && !((t2 - t) % l)) return cout << t2 << '\n', 0;
    if (~l2 && t - t2 >= 0 && !((t - t2) % l2)) return cout << t << '\n', 0;
    if (l == -1 || l2 == -1) return cout << "-1\n", 0;
    int val = t2 - t;
    for (int i = 1, lim = m << 1; i <= lim; ++i)
        if (val + 1ll * i * l2 >= 0 && !((val + 1ll * i * l2) % l)) return cout << 1ll * i * l2 + t2 << '\n', 0;
    cout << "-1\n";
    return 0;
}