#include "_.h"

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

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

    int count_safe = 0;
	for (int i = 0; inputs[i] != NULL; i++) {
	    char **inputs_space_first = str_split(strdup(inputs[i]), ' ');
        int is_safe = 0;
        for (int y = -1; inputs_space_first[y] != NULL; y++) {
            if (is_safe == 1)
                break;
            int increase = 0;
            is_safe = 1;
	        char **inputs_space = str_split(strdup(inputs[i]), ' ');
            if (y > -1)
                inputs_space[y] = "-1";
            for (int j = 0; inputs_space[j + 1] != NULL; j++) {
                int number = atoi(inputs_space[j]);
                int next_number = atoi(inputs_space[j + 1]);
                if (number == -1) {
                    if (inputs_space[j + 2] == NULL)
                        break;
                    number = atoi(inputs_space[j + 1]);
                    next_number = atoi(inputs_space[j + 2]);
                }
                if (next_number == -1) {
                    if (inputs_space[j + 2] == NULL)
                        break;
                    next_number = atoi(inputs_space[j + 2]);
                }

                if (number - next_number > 0 && number - next_number < 4 && (increase == 1 || increase == 0)) {
                    increase = 1;
                }
                else if (number - next_number < 0 && number - next_number > -4 && (increase == -1 || increase == 0)) {
                    increase = -1;
                }
                else {
                    is_safe = 0;
                    break;
                }
            }
        }
        if (is_safe == 1)
            count_safe++;
	}
    
    printf("safe: %d\n", count_safe);

	return 0;
}
