import java.util.*;
class Node {
public int data;
public Node next;
Node(int d)
{
data = d;
next = null;
}
}
class Solution
{
Node intersectionOfTwoLinkedLists(Node l1, Node l2)
{
while(l1!=null)
{
while (l2!=null)
{
if(l1.data==l2.data)
{
return l1;
}
l2=l2.next;
}
l1=l1.next;
}
return null;
}
}
public class Main {
static Scanner sc = new Scanner(System.in);
public static Node buildList(HashMap<Integer, Node> hash) {
int x = sc.nextInt();
Node head = new Node(x);
Node current = head;
hash.put(x, head);
while (x != -1) {
x = sc.nextInt();
if (x == -1)
break;
Node n = new Node(x);
hash.put(x, n);
current.next = n;
current = n;
}
current.next = null;
return head;
}
public static void printLinkedList(Node head) {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
HashMap<Integer, Node> hash = new HashMap<Integer, Node>();
Node l1 = buildList(hash);
Node l2 = null;
int x = sc.nextInt();
l2 = new Node(x);
Node temp = l2;
while (x != -1) {
x = sc.nextInt();
if (x == -1)
break;
if (hash.containsKey(x)) {
temp.next = hash.get(x);
break;
}
Node n = new Node(x);
temp.next = n;
temp = n;
}
System.out.print("L1 - ");
printLinkedList(l1);
System.out.print("L2 - ");
printLinkedList(l2);
Solution s = new Solution();
Node intersectionPoint = s.intersectionOfTwoLinkedLists(l1, l2);
System.out.println("Intersection at node with data = " + intersectionPoint.data);
}
}