Moose Calf
Recently, a magical forest appeared at the edge of the Great Flatland Reserve, and a very curious young moose decided to explore it. After several hours of wandering among the enchanted trees, the young moose emerged from the forest, now much less curious. However, upon returning home, the moose discovered that its antlers had significantly widened. Since larger antlers signify greater influence in moose society, the other moose demanded a detailed map of the magical forest from the young moose.
According to the young moose, who surprisingly remembered everything, the forest is divided into N parallel rows, with k_i trees in the i-th row. The moose accurately noted the distances between neighboring trees in each row. Because the forest is magical, when the moose passed between trees with a distance d between them, its antlers grew to d.
Due to their straightforward nature and careful regard for their antlers, all the moose agreed that one can only move forward through the magical forest—from row i to i+1. No moose can pass between trees if the distance between them is less than the width of its antlers. Now, all the moose, especially those with small antlers, are curious about the maximum width of antlers a moose can have when it exits the forest, assuming that before entering the forest, the width of its antlers was zero.
Input
The first line contains a single number N—the number of rows of trees in the forest, where 2 ≤ N ≤ 10^3. The next N lines describe the rows. The first number in the i-th line is k_i—the number of trees in the i-th row, where 2 ≤ k_i ≤ 10^3. Following this are k_i-1 positive integers, each not exceeding 10000, representing the distances between neighboring trees in the i-th row.
Output
Output a single number—the maximum length of the moose's antlers if it is possible to exit the forest, or -1 if it is impossible to exit the forest.