#include <iostream>
#include<vector>

using namespace std;


#define SIZE (100)

std::pair<int,int> longest(vector<vector<int>>& E, vector<pair<int,int>>& R, int n) {
    if(E[n].size() == 0)
        return make_pair(0,n);

    if(R[n].first != -1)
        return R[n];

    for(auto& e : E[n]) {
        std::pair<int,int> p = longest(E,R,e);
        if(p.first + 1 >  R[n].first || (p.first+1==R[n].first && p.second < R[n].second)) {
            R[n].first = p.first+1;
            R[n].second=p.second;
        }
    }
    return R[n];

}


int main() {
    int n; cin>>n;
    int T=0;
    while(n > 0) {
        n++;
        vector<vector<int>> E(n,vector<int>());
        vector<pair<int,int>> R(n,pair<int,int>(-1,-1));
        int s; cin>>s;
        int a,b; cin>>a>>b;
        while(a!=0 && b!=0) {
            E[a].push_back(b);
            cin>>a>>b;
        }
        auto p = longest(E,R,s);
        cout<<"Case "<<++T<<": The longest path from "<<s<<" has length "<<p.first<<", finishing at "<<p.second<<"."<<endl<<endl;
        cin>>n;
    }
}

