#include <stdio.h>
#include <stdlib.h>

int points(int n, int reqs[1][16]){

	int mass = 0;
	int last_ball[n] = {0, 0, 0, 0, 0, 0, 0, 0};

	for(int i = 0; i < n; i++){

		if(reqs[1][i] > 0){

			mass = mass + reqs[1][reqs[0][i]] + reqs[1][i];
			last_ball[i] = reqs[1][i];

		}
		if(reqs[1][i] == 0){

			mass = mass - last_ball[reqs[0][i]];

		}

	}

	printf("%d", mass*2);

}

int main()
{

    int n;
    int requests[2][16];

    /*requests[0][0] = 0;
    requests[0][1] = 1;
    requests[0][2] = 2;
    requests[0][3] = 3;
    requests[0][4] = 4;
    requests[0][5] = 5;
    requests[0][6] = 6;
    requests[0][7] = 1;

    requests[1][0] = 1;
    requests[1][1] = 5;
    requests[1][2] = 4;
    requests[1][3] = 2;
    requests[1][4] = 3;
    requests[1][5] = 0;
    requests[1][6] = 6;
    requests[1][7] = 0;*/

    scanf("%d", &n);

    for(int i = 0; i < n; i++){

		scanf("%d", &requests[0][i]);
		scanf("%d", &requests[1][i]);

    }

    points(n, requests);

}
