#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define all(a) a.begin(),a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back #define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n' #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef long double ld; typedef pair P; typedef complex com; template using prique = priority_queue, greater>; constexpr int inf = 1000000010; constexpr ll INF = 1000000000000000010; constexpr int mod1e9 = 1000000007; constexpr int mod998 = 998244353; constexpr ld eps = 1e-12; constexpr ld pi = 3.141592653589793238; constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; }; int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 }; void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); } template inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } template istream &operator >> (istream &s, vector &v) { for (auto &e : v) s >> e; return s; } template ostream &operator << (ostream &s, const vector &v) { for (auto &e : v) s << e << ' '; return s; } struct fastio { fastio() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(20); } }fastio_; ll modinv(ll a, ll mod) { a %= mod; if (a == 0) abort(); ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } int main() { int n, p; cin >> n >> p; vector a(n), b(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; int check = 0; check += count(a.begin(), a.end(), 0) == n; check += count(b.begin(), b.end(), 0) == n; vector d = a; if (check == 2) { cout << "0\n"; return 0; } if (check == 1) { cout << "-1\n"; return 0; } if (n == 1) { int dbl = 0; while (a[0] != b[0]) a[0] *= 2, a[0] %= p, dbl++; cout << dbl + 1 << '\n'; rep(_, dbl) cout << "1 1 1\n"; cout << "2\n"; return 0; } vector powtwo; int memo = 1; rep(_, 2100) { powtwo.pb(memo); memo *= 2; memo %= p; } vector

table(p); rep(i, 2100) { for (int j = i; j < 2100; j++) { table[(powtwo[i] + powtwo[j]) % p] = { i,j }; } } vector

ans; int ax = -1, bx = -1; rep(i, n) { if (a[i] != 0) ax = i; if (b[i] != 0) bx = i; } if (ax == bx) { int ay = ax + 1; if (ay == n) ay = 0; if (a[ay] == 0) { ans.pb({ ay,ax }); a[ay] += a[ax]; a[ay] %= p; } ax = ay; } ll cnt = (b[bx] * modinv(214472, p) % p - a[bx] + p) * modinv(a[ax], p) % p; if (cnt) { rep(_, table[cnt].first) { ans.pb({ ax,ax }); a[ax] *= 2; a[ax] %= p; } ans.pb({ bx,ax }); a[bx] += a[ax]; a[bx] %= p; rep(_, table[cnt].second - table[cnt].first) { ans.pb({ ax,ax }); a[ax] *= 2; a[ax] %= p; } ans.pb({ bx,ax }); a[bx] += a[ax]; a[bx] %= p; } vector plus(n); rep(i, n) plus[i] = (b[i] - a[i] + p) * modinv(a[bx], p) % p; vector> change(2100); rep(i, n) { if (i == bx) continue; change[table[plus[i]].first].pb(i); change[table[plus[i]].second].pb(i); } rep(i, 2100) { for (int j : change[i]) ans.pb({ j,bx }); ans.pb({ bx,bx }); } cout << ans.size() + 1 << '\n'; for (P pa : ans) cout << 1 << ' ' << pa.first << ' ' << pa.second << '\n'; cout << "2\n"; }