module.exports=function(e){var r={};function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var o in e)t.d(n,o,function(r){return e[r]}.bind(null,o));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(e,r,t){t(1)(t(2).readFileSync("/dev/stdin","UTF-8"))},function(e,r){e.exports=function(e){for(var r=e.split("\n"),t=Number(r[0]),n=[],o=[],u=0;u<t;u++){var i=r[u+1].split(" ").map(function(e){return Number(e)-1});o.push(i[0]),n.push(i)}for(var f=t*(t-1)/2,l=new Array(t).fill(0),a=new Array(t).fill(0),c=[],p=0;p<t;p++)c.push(p);for(var s=0,d=0;c.length>0;){var b=c.shift();if(o[o[b]]==b){d+=1;var v=o[b],y=Math.max(a[b],a[v])+1;s<y&&(s=y),a[b]=y,a[v]=y,l[b]+=1,l[v]+=1,l[b]!=t&&(o[b]=n[b][l[b]],c.push(b)),l[v]!=t&&(o[v]=n[v][l[v]],c.push(v))}}d==f?console.log(s):console.log(-1)}},function(e,r){e.exports=require("fs")}]);