import java.io.*;
import java.util.Locale;
import java.util.StringTokenizer;
public class TaskA_ok {
private final InputReader reader;
private final OutputWriter writer;
public TaskA_ok(InputReader reader, OutputWriter writer) {
this.reader = reader;
this.writer = writer;
}
public static void main
(String[] args
) { InputReader reader
= new InputReader
(System.
in); OutputWriter writer
= new OutputWriter
(System.
out); new TaskA_ok(reader, writer).run();
writer.writer.flush();
}
public void run() {
long p = reader.nextLong();
long v1 = reader.nextLong();
long t1 = reader.nextLong();
long k1 = reader.nextLong();
long b1 = reader.nextLong();
long v2 = reader.nextLong();
long t2 = reader.nextLong();
long k2 = reader.nextLong();
long b2 = reader.nextLong();
long f = 0;
while (v1 != t1) {
v1 = (v1 * k1 + b1) % p;
v2 = (v2 * k2 + b2) % p;
f++;
if (f > p + 1) {
writer.printf("-1\n");
return;
}
}
if (v2 == t2) {
writer.printf("%d\n", f);
return;
}
long per = 0;
long pk2 = 1, pb2 = 0;
while (per == 0 || v1 != t1) {
v1 = (v1 * k1 + b1) % p;
pk2 = (pk2 * k2) % p;
pb2 = (pb2 * k2 + b2) % p;
per++;
if (per > p + 1) {
writer.printf("%d\n", -1);
return;
}
}
long f2 = 0;
while (v2 != t2) {
f2++;
v2 = (pk2 * v2 + pb2) % p;
if (f2 > p + 1) {
writer.printf("-1\n");
return;
}
}
writer.printf("%d\n", f + per * f2);
}
static class InputReader {
tokenizer = null;
}
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
}
}
return tokenizer.nextToken();
}
public int nextInt() {
}
public double nextDouble() {
return Double.
parseDouble(next
()); }
public long nextLong() {
return Long.
parseLong(next
()); }
}
static class OutputWriter {
}
}
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC5Mb2NhbGU7CmltcG9ydCBqYXZhLnV0aWwuU3RyaW5nVG9rZW5pemVyOwoKcHVibGljIGNsYXNzIFRhc2tBX29rIHsKICAgIHByaXZhdGUgZmluYWwgSW5wdXRSZWFkZXIgcmVhZGVyOwogICAgcHJpdmF0ZSBmaW5hbCBPdXRwdXRXcml0ZXIgd3JpdGVyOwoKICAgIHB1YmxpYyBUYXNrQV9vayhJbnB1dFJlYWRlciByZWFkZXIsIE91dHB1dFdyaXRlciB3cml0ZXIpIHsKICAgICAgICB0aGlzLnJlYWRlciA9IHJlYWRlcjsKICAgICAgICB0aGlzLndyaXRlciA9IHdyaXRlcjsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgSW5wdXRSZWFkZXIgcmVhZGVyID0gbmV3IElucHV0UmVhZGVyKFN5c3RlbS5pbik7CiAgICAgICAgT3V0cHV0V3JpdGVyIHdyaXRlciA9IG5ldyBPdXRwdXRXcml0ZXIoU3lzdGVtLm91dCk7CiAgICAgICAgbmV3IFRhc2tBX29rKHJlYWRlciwgd3JpdGVyKS5ydW4oKTsKICAgICAgICB3cml0ZXIud3JpdGVyLmZsdXNoKCk7CiAgICB9CgogICAgcHVibGljIHZvaWQgcnVuKCkgewogICAgICAgIGxvbmcgcCA9IHJlYWRlci5uZXh0TG9uZygpOwogICAgICAgIGxvbmcgdjEgPSByZWFkZXIubmV4dExvbmcoKTsKICAgICAgICBsb25nIHQxID0gcmVhZGVyLm5leHRMb25nKCk7CiAgICAgICAgbG9uZyBrMSA9IHJlYWRlci5uZXh0TG9uZygpOwogICAgICAgIGxvbmcgYjEgPSByZWFkZXIubmV4dExvbmcoKTsKICAgICAgICBsb25nIHYyID0gcmVhZGVyLm5leHRMb25nKCk7CiAgICAgICAgbG9uZyB0MiA9IHJlYWRlci5uZXh0TG9uZygpOwogICAgICAgIGxvbmcgazIgPSByZWFkZXIubmV4dExvbmcoKTsKICAgICAgICBsb25nIGIyID0gcmVhZGVyLm5leHRMb25nKCk7CgogICAgICAgIGxvbmcgZiA9IDA7CiAgICAgICAgd2hpbGUgKHYxICE9IHQxKSB7CiAgICAgICAgICAgIHYxID0gKHYxICogazEgKyBiMSkgJSBwOwogICAgICAgICAgICB2MiA9ICh2MiAqIGsyICsgYjIpICUgcDsKICAgICAgICAgICAgZisrOwogICAgICAgICAgICBpZiAoZiA+IHAgKyAxKSB7CiAgICAgICAgICAgICAgICB3cml0ZXIucHJpbnRmKCItMVxuIik7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHYyID09IHQyKSB7CiAgICAgICAgICAgIHdyaXRlci5wcmludGYoIiVkXG4iLCBmKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBsb25nIHBlciA9IDA7CiAgICAgICAgbG9uZyBwazIgPSAxLCBwYjIgPSAwOwogICAgICAgIHdoaWxlIChwZXIgPT0gMCB8fCB2MSAhPSB0MSkgewogICAgICAgICAgICB2MSA9ICh2MSAqIGsxICsgYjEpICUgcDsKICAgICAgICAgICAgcGsyID0gKHBrMiAqIGsyKSAlIHA7CiAgICAgICAgICAgIHBiMiA9IChwYjIgKiBrMiArIGIyKSAlIHA7CiAgICAgICAgICAgIHBlcisrOwogICAgICAgICAgICBpZiAocGVyID4gcCArIDEpIHsKICAgICAgICAgICAgICAgIHdyaXRlci5wcmludGYoIiVkXG4iLCAtMSk7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgbG9uZyBmMiA9IDA7CiAgICAgICAgd2hpbGUgKHYyICE9IHQyKSB7CiAgICAgICAgICAgIGYyKys7CiAgICAgICAgICAgIHYyID0gKHBrMiAqIHYyICsgcGIyKSAlIHA7CiAgICAgICAgICAgIGlmIChmMiA+IHAgKyAxKSB7CiAgICAgICAgICAgICAgICB3cml0ZXIucHJpbnRmKCItMVxuIik7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgd3JpdGVyLnByaW50ZigiJWRcbiIsIGYgKyBwZXIgKiBmMik7CiAgICB9CgoKICAgIHN0YXRpYyBjbGFzcyBJbnB1dFJlYWRlciB7CiAgICAgICAgcHVibGljIEJ1ZmZlcmVkUmVhZGVyIHJlYWRlcjsKICAgICAgICBwdWJsaWMgU3RyaW5nVG9rZW5pemVyIHRva2VuaXplcjsKCiAgICAgICAgcHVibGljIElucHV0UmVhZGVyKElucHV0U3RyZWFtIHN0cmVhbSkgewogICAgICAgICAgICByZWFkZXIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKHN0cmVhbSksIDMyNzY4KTsKICAgICAgICAgICAgdG9rZW5pemVyID0gbnVsbDsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBTdHJpbmcgbmV4dCgpIHsKICAgICAgICAgICAgd2hpbGUgKHRva2VuaXplciA9PSBudWxsIHx8ICF0b2tlbml6ZXIuaGFzTW9yZVRva2VucygpKSB7CiAgICAgICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgICAgICAgIHRva2VuaXplciA9IG5ldyBTdHJpbmdUb2tlbml6ZXIocmVhZGVyLnJlYWRMaW5lKCkpOwogICAgICAgICAgICAgICAgfSBjYXRjaCAoSU9FeGNlcHRpb24gZSkgewogICAgICAgICAgICAgICAgICAgIHRocm93IG5ldyBSdW50aW1lRXhjZXB0aW9uKGUpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB0b2tlbml6ZXIubmV4dFRva2VuKCk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgaW50IG5leHRJbnQoKSB7CiAgICAgICAgICAgIHJldHVybiBJbnRlZ2VyLnBhcnNlSW50KG5leHQoKSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgZG91YmxlIG5leHREb3VibGUoKSB7CiAgICAgICAgICAgIHJldHVybiBEb3VibGUucGFyc2VEb3VibGUobmV4dCgpKTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBsb25nIG5leHRMb25nKCkgewogICAgICAgICAgICByZXR1cm4gTG9uZy5wYXJzZUxvbmcobmV4dCgpKTsKICAgICAgICB9CiAgICB9CgogICAgc3RhdGljIGNsYXNzIE91dHB1dFdyaXRlciB7CiAgICAgICAgcHVibGljIFByaW50V3JpdGVyIHdyaXRlcjsKCiAgICAgICAgT3V0cHV0V3JpdGVyKE91dHB1dFN0cmVhbSBzdHJlYW0pIHsKICAgICAgICAgICAgd3JpdGVyID0gbmV3IFByaW50V3JpdGVyKHN0cmVhbSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgdm9pZCBwcmludGYoU3RyaW5nIGZvcm1hdCwgT2JqZWN0Li4uIGFyZ3MpIHsKICAgICAgICAgICAgd3JpdGVyLnByaW50KFN0cmluZy5mb3JtYXQoTG9jYWxlLkVOR0xJU0gsIGZvcm1hdCwgYXJncykpOwogICAgICAgIH0KICAgIH0KfQo=