/* in Java, 64MB is just a little bit too little */ import java.util.*; public class MengerSpongeRecursiveGodmar { public static void main(String []av) { var sc = new Scanner(System.in); long L = sc.nextLong(); Fraction x = new Fraction(sc.nextLong(), sc.nextLong()); Fraction y = new Fraction(sc.nextLong(), sc.nextLong()); Fraction z = new Fraction(sc.nextLong(), sc.nextLong()); System.out.println(menger_sponge(L, x, y, z)); } static Fraction t1 = new Fraction(1, 3); static Fraction t2 = new Fraction(2, 3); static int menger_sponge(long L, Fraction x, Fraction y, Fraction z) { if (L == 0) return 1; boolean cx = t1.compareTo(x) == -1 && x.compareTo(t2) == -1; boolean cy = t1.compareTo(y) == -1 && y.compareTo(t2) == -1; boolean cz = t1.compareTo(z) == -1 && z.compareTo(t2) == -1; if (cx && cy || cx && cz || cy && cz) return 0; return menger_sponge(L-1, scale(x), scale(y), scale(z)); } static Fraction scale(Fraction x) { if (x.compareTo(t1) == -1) return x.mul(3); else if (x.compareTo(t2) == -1) return x.sub(t1).mul(3); else return x.sub(t2).mul(3); } static class Fraction implements Comparable { final long n, d; Fraction(long n, long d) { this.n = n; this.d = d; if (d == 0) throw new Error("denominator is zero"); } Fraction mul(long s) { return new Fraction(s * n, d); } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a%b); } Fraction sub(Fraction that) { long g = gcd(this.d, that.d); long lcm = this.d * that.d / g; return new Fraction(n*that.d/g - that.n*this.d/g, lcm); } @Override public int compareTo(Fraction that) { return Long.compare(this.n*that.d, this.d*that.n); } } }