PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000298. Định lý số dư Trung Hoa (Chinese Remainder)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Định lý số dư Trung Hoa (Chinese Remainder)

Chinese Remainder

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Given four integers a,n,b,ma,n,b,m, consider the two congruences

x≡a(modn),x≡b(modm).x\equiv a\pmod n,\qquad x\equiv b\pmod m.

It is guaranteed that gcd⁡(n,m)=1\gcd(n,m)=1. Therefore, exactly one integer xx in 0≤x<nm0\le x<nm satisfies both congruences.

Find this xx and also output the common modulus nmnm.

Input

A single line containing a,n,b,ma,n,b,m, in that order.

Output

Print two space-separated integers: xx and nmnm.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤1001\le n,m\le 100; 0≤a<n0\le a<n; 0≤b<m0\le b<m; gcd⁡(n,m)=1\gcd(n,m)=1; nm≤1018nm\le10^{18}.
  • Subtask 2 (30 points): 1≤n,m≤1000001\le n,m\le 100000; 0≤a<n0\le a<n; 0≤b<m0\le b<m; gcd⁡(n,m)=1\gcd(n,m)=1; nm≤1018nm\le10^{18}.
  • Subtask 3 (50 points): 1≤n,m≤10000000001\le n,m\le 1000000000; 0≤a<n0\le a<n; 0≤b<m0\le b<m; gcd⁡(n,m)=1\gcd(n,m)=1; nm≤1018nm\le10^{18}.

Examples

Examples 1

Input

2 3 3 5

Output

8 15

Explanation

Since 8 mod 3=28\bmod 3=2 and 8 mod 5=38\bmod 5=3, and all other solutions differ by a multiple of 1515, the output is 8 15.

Examples 2

Input

0 7 3 5

Output

28 35

Explanation

The integer 2828 is divisible by 77 and leaves remainder 33 modulo 55. The combined modulus is 7⋅5=357\cdot5=35.