#include "_.h"

long long solve(long long A[], long long B[], long long prize[]) {

    if (((prize[0] * B[1]) - (B[0] * prize[1])) % ((A[0] * B[1]) - (B[0] * A[1])) != 0)
        return 0;

    if (((A[0] * prize[1]) - (prize[0] * A[1])) % ((A[0] * B[1]) - (B[0] * A[1])) != 0)
        return 0;

    long long x = ((prize[0] * B[1]) - (B[0] * prize[1])) / ((A[0] * B[1]) - (B[0] * A[1]));
    long long y = ((A[0] * prize[1]) - (prize[0] * A[1])) / ((A[0] * B[1]) - (B[0] * A[1]));

    if (x <= 0 || y <= 0)
        return 0;

    long long result = x * 3 + y;
    return result;
}


int main(int argc, char **argv) {
	
	if (argc != 2)
		return 1;

	char *input = argv[1];
	char **inputs = str_split(strdup(input), '\n');

    long long token_spent = 0;
	for (long long i = 0; inputs[i] != NULL; i+=3) {
	    char **button_A = str_split(strdup(inputs[i]), ' ');
        long long A[2] = {atoi(button_A[0]), atoi(button_A[1])};  

	    char **button_B = str_split(strdup(inputs[i + 1]), ' ');
        long long B[2] = {atoi(button_B[0]), atoi(button_B[1])};  

	    char **prizes = str_split(strdup(inputs[i + 2]), ' ');
        long long prize[2] = {atoi(prizes[0]) + 10000000000000, atoi(prizes[1]) + 10000000000000};  

        token_spent += solve(A, B, prize);
	}

    printf("token_spent: %lld\n", token_spent);

	return 0;
}
