/* 
* Validator задачи ОЧЕРЕДЬ.
*/
#include "testlib.h"
using namespace std;
const int nMAX = 2000000;
int a[nMAX+1], b[nMAX+1];
int x[nMAX+1], y[nMAX+1], s[nMAX+1];

int main(int argc, char* argv[])
{
    registerValidation(argc, argv);
    int n;
    if (validator.group() == "task1") n = inf.readInt(2, 40000, "n");
        else n = inf.readInt(2, nMAX, "n");
        
    inf.readEoln();    
    for(int i = 1; i <= n; i++) 
    {   
        x[i] = 0; y[i] = 0; s[i] = 0;
    }

    for(int i = 1; i < n; i++)
    {   
        a[i] = inf.readInt(1, n);   inf.readSpace();
        b[i] = inf.readInt(1, n);   inf.readEoln();
        if (a[i] == b[i]) quitf(_pe,"numbers of lines %d are equal", i);
        x[a[i]]++;  y[b[i]]++;
    }

    for(int i = 1; i <= n; i++)
    {
        if (x[i] > 1) quitf(_pe,"first number %d used %d times",  i, x[i]);
        if (y[i] > 1) quitf(_pe,"second number %d used %d times", i, y[i]);
    }

    for(int i = 1; i <= n; i++)  x[i] = 0;
    for(int i = 1; i < n;  i++)  x[a[i]] = i;

    int t;
    for(int i = 1; i <= n; i++) 
        if (y[i] == 0) t = x[i];

    s[a[t]]++;  s[b[t]]++;
    for(int i = 1; i < n - 1; i++)
    {
        t = x[b[t]];   s[b[t]]++;
    }

    for(int i = 1; i <= n; i++)
        if (s[i] != 1) quitf(_pe,"set of numbers is not queue");

    inf.readEof();    
    return 0;
}