// hirlanc.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <vector>
using namespace std;
struct adat {
vector<int>be;
int sz, tav;
bool lat, kor;
};
vector<adat>x, y;
vector<int>csp;
pair<int, int>sol;
int n, hossz, db, tav;
bool kor(int akt, int kezd)
{
x[akt].lat = true;
++tav;
if (x[x[akt].sz].kor)
{
x[akt].lat = false;
x[akt].kor = false;
return false;
}
else
{
if (x[akt].sz == kezd)
{
x[x[akt].sz].tav = tav + 1;
x[akt].tav = tav;
x[x[akt].sz].kor = true;
x[akt].kor = true;
return true;
}
else if (x[x[akt].sz].lat)
{
x[akt].lat = false;
x[akt].kor = false;
return false;
}
if (kor(x[akt].sz, kezd))
{
x[akt].tav = tav;
x[akt].kor = true;
}
else
{
x[akt].lat = false;
x[akt].kor = false;
return false;
}
}
}
void bejar(int akt, int tav)
{
x[akt].lat = 1;
x[akt].tav = tav;
for (auto& e : x[akt].be)
{
if (!x[e].lat && !x[e].kor)
{
bejar(e, tav + 1);
}
}
}
//void bejar(int akt)
//{
// x[akt].lat = true;
// y[akt].lat = true;
// ++hossz;
// if (x[x[akt].sz].lat == false)
// {
// bejar(x[akt].sz);
// }
// x[akt].lat = false;
// return;
//}
//
//void kor(int akt, int kezd)
//{
// y[akt].lat = true;
// ++hossz;
// if (y[y[akt].sz].lat == false)
// {
// kor(y[akt].sz, kezd);
// }
//}
int main()
{
cin >> n;
x.resize(n + 1);
//y.resize(n + 1, { 0,0,0 });
int a = 0;
for (int i = 1; i <= n; ++i)
{
cin >> a;
x[i].sz = a;
x[a].be.push_back(i);
}
for (int i = 1; i <= n; ++i)
{
tav = 0;
if (!x[i].kor) kor(i, i);
}
for (int i = 1; i <= n; ++i)
{
if (x[i].kor) bejar(i, x[i].tav);
if (x[i].tav > sol.second)
{
sol.first = i;
sol.second = x[i].tav;
}
}
/*for (int i = 1; i <= n; ++i)
{
cout << i << ": " << x[i].tav << endl;
}*/
//for (int i = 1; i <= n; ++i)
//{
// if (x[i].be == 0)
// {
// csp.push_back(i);
// }
//}
//sol.first = 0;
//sol.second = 0;
//for (auto& e : csp)
//{
// bejar(e);
// if (hossz > sol.second)
// {
// sol.first = e;
// sol.second = hossz;
// }
// hossz = 0;
//}
//for (int i = 1; i <= n; ++i)
//{
// if (y[i].lat == 0) ++db;
//}
//for (int i = 1; i <= n; ++i)
//{
// if (db <= sol.second)
// {
// break;
// }
// else
// {
// if (y[i].lat == false)
// {
// kor(i, i);
// if (hossz > sol.second)
// {
// sol.first = i;
// sol.second = hossz;
// }
// db += hossz;
// hossz = 0;
// }
// }
//}
cout << sol.first << " " << sol.second;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file