// Arup Guha
// 11/10/2014
// Solution to 2013 November USACO Bronze Problem: Combination Lock.

import java.util.*;
import java.io.*;

public class combo {

    public static void main(String[] args) throws Exception {

        Scanner stdin = new Scanner(new File("combo.in"));
        int n = stdin.nextInt();
        int[] combo1 = new int[3];
        int[] combo2 = new int[3];

        // Read in both combinations.
        for (int i=0; i<3; i++)
            combo1[i] = stdin.nextInt()%n;
        for (int i=0; i<3; i++)
            combo2[i] = stdin.nextInt()%n;

        // Count all the combinations that open either.
        int cnt = 0;
        for (int i=0; i<n*n*n; i++)
            if (open(i,combo1,n) || open(i,combo2,n))
                cnt++;

        // Output the result.
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("combo.out")));
        out.println(cnt);
        out.close();
        stdin.close();
    }

    public static boolean open(int combo, int[] lock, int n) {

        // Go through all three.
        for (int i=0; i<3; i++) {

            // Be careful with mod when checking wrap-arounds!
            if ( (n+combo%n - lock[i])%n > 2 && (n+combo%n - lock[i])%n  < n-2)
                return false;
            combo /= n;
        }

        // If we get here, it works!
        return true;
    }
}
