public class Fraction {
	long n;
	long d;

	public Fraction(long n, long d) {
		this.n = n;
		this.d = d;
	}
	
	public Fraction(String a) {
		String[] nd = a.split("/", 2);
		if (nd.length == 1) {
			this.n = Long.parseLong(nd[0]);
			this.d = 1;
		}
		else if (nd.length == 2) {
			this.n = Long.parseLong(nd[0]);
			this.d = Long.parseLong(nd[1]);
		}
	}

	public Fraction add(Fraction a) {
		long n = this.d * a.n + a.d * this.n;
		long d = this.d * a.d;
		return new Fraction(n,d).reduce();
	}

	public Fraction subtract(Fraction a) {
		long n = a.d * this.n - this.d * a.n; // BLANK_0
		long d = this.d * a.d; // BLANK_1
		return new Fraction(n,d).reduce();
	}

	public Fraction multiply(Fraction a) {
		long n = this.n * a.n; // BLANK_2
		long d = this.d * a.d; // BLANK_3
		return new Fraction(n,d).reduce();
	}

	public Fraction divide(Fraction a) {
		long n = this.n * a.d; // BLANK_4
		long d = this.d * a.n; // BLANK_5
		return new Fraction(n,d).reduce();
	}

	public Fraction reduce() {
		long gcd = gcd(this.n, this.d);
		if (gcd != 0) {
			this.n /= gcd; // BLANK_6
			this.d /= gcd; // BLANK_7
		}
		return this;
	}

	public static long gcd(long a, long b) {
		return b == 0 ? a : gcd(b, a % b);
	}

	public String toString() {
		if (d == -1) return -this.n + "";
		if (d == 1) return this.n + "";
		if (n == 0) return "0";
		
		if (this.d < 0)
			return -this.n + "/" + -this.d;
		else
			return this.n + "/" + this.d;
	}
	
	public boolean equals(Fraction a) {
		a.reduce();
		reduce();
		return (a.n == n) && (a.d == d);
	}
}