#include <iostream>
using namespace std;
int main() {
int quantity, instrument, counter = 1;
cin >> quantity >> instrument;
if ((quantity == 6 && instrument != 1) || (quantity == instrument && quantity % 2 == 0)) {
cout << 0;
} else if (quantity == instrument) {
for (int num = 1; num % quantity != 0; num += 6) {
if (num != 1 && num % quantity == 1) {
cout << 0;
return 0;
} else {
counter++;
}
}
cout << counter;
} else {
for (int num = 1; num % quantity != instrument; num += 6) {
if (num != 1 && num % quantity == 1) {
cout << 0;
return 0;
} else {
counter++;
}
}
cout << counter;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CglpbnQgcXVhbnRpdHksIGluc3RydW1lbnQsIGNvdW50ZXIgPSAxOwoJY2luID4+IHF1YW50aXR5ID4+IGluc3RydW1lbnQ7CglpZiAoKHF1YW50aXR5ID09IDYgJiYgaW5zdHJ1bWVudCAhPSAxKSB8fCAocXVhbnRpdHkgPT0gaW5zdHJ1bWVudCAmJiBxdWFudGl0eSAlIDIgPT0gMCkpIHsKCQljb3V0IDw8IDA7Cgl9IGVsc2UgaWYgKHF1YW50aXR5ID09IGluc3RydW1lbnQpIHsKCQlmb3IgKGludCBudW0gPSAxOyBudW0gJSBxdWFudGl0eSAhPSAwOyBudW0gKz0gNikgewoJCQlpZiAobnVtICE9IDEgJiYgbnVtICUgcXVhbnRpdHkgPT0gMSkgewoJCQkJY291dCA8PCAwOwoJCQkJcmV0dXJuIDA7CgkJCX0gZWxzZSB7CgkJCQljb3VudGVyKys7CgkJCX0JCgkJfQoJCWNvdXQgPDwgY291bnRlcjsKCX0gZWxzZSB7CgkJZm9yIChpbnQgbnVtID0gMTsgbnVtICUgcXVhbnRpdHkgIT0gaW5zdHJ1bWVudDsgbnVtICs9IDYpIHsKCQkJaWYgKG51bSAhPSAxICYmIG51bSAlIHF1YW50aXR5ID09IDEpIHsKCQkJCWNvdXQgPDwgMDsKCQkJCXJldHVybiAwOwoJCQl9IGVsc2UgewoJCQkJY291bnRlcisrOwoJCQl9CQoJCX0KCQljb3V0IDw8IGNvdW50ZXI7Cgl9CglyZXR1cm4gMDsKfQ==