#include <stdbool.h> #include <stdio.h> #include <stdlib.h> static bool after[100][100]; int compare(const void *x, const void *y) { return after[*(const unsigned char *) x][*(const unsigned char *) y]; } int main() { unsigned char x, y; while (scanf("%hhu|%hhu\n", &x, &y) == 2) after[y][x] = true; unsigned char line[90]; unsigned sum = y = 0; do { bool right = true; do { line[y] = x; for (unsigned char i = 0; right && i < y; ++i) if (after[line[i]][line[y]]) right = false; } while (scanf(",%hhu", &x) == 1 && ++y); if (right) continue; qsort(line, ++y, 1, compare); sum += line[y >> 1]; } while (y = 0, scanf("%hhu", &x) == 1); printf("%u\n", sum); return 0; }