#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 *list_one = calloc(strlen(input) + 1, sizeof(int *));
    int *list_two = calloc(strlen(input) + 1, sizeof(int *));


    int i = 0;
    for (i = 0; inputs[i] != NULL; i++) {
        char **inputs_int = str_split(strdup(inputs[i]), ' ');
        list_one[i] = atoi(inputs_int[0]);
        list_two[i] = atoi(inputs_int[1]);
    }

    list_one[i] = -2;
    list_two[i] = -2;

    int distance = 0;

    for (i = 0; list_one[i] != -2; i++) {

        int min_one = INT_MAX;
        int min_two = INT_MAX;
        int min_one_index = 0;
        int min_two_index = 0;

        for (int j = 0; list_two[j] != -2; j++) {
            if (list_one[j] != -1) {
                min_one = MIN(list_one[j], min_one);
                if (list_one[j] == min_one)
                    min_one_index = j;
            }
            if (list_two[j] != -1) {
                min_two = MIN(list_two[j], min_two);
                if (list_two[j] == min_two)
                    min_two_index = j;
            }
        }

        distance += ABS(min_one - min_two);
        list_one[min_one_index] = -1;
        list_two[min_two_index] = -1;
    }

    printf("distance: %d\n", distance);

    return 0;
}
