1629 {
1630 auto record = unordered_map<int, vector<int>>();
1631 for(int i = 1; i <= n; i++) {
1632 record.insert(pair(i, vector<int>()));
1633 }
1634 auto um = unordered_map<int, unordered_set<int>>();
1635 for(auto edge: edges) {
1636 if(!um.contains(edge[0])) {
1637 auto us = unordered_set<int>();
1638 us.insert(edge[1]);
1639 um.insert(pair(edge[0], us));
1640 } else {
1641 um[edge[0]].insert(edge[1]);
1642 }
1643 if(!um.contains(edge[1])) {
1644 auto us = unordered_set<int>();
1645 us.insert(edge[0]);
1646 um.insert(pair(edge[1], us));
1647 } else {
1648 um[edge[1]].insert(edge[0]);
1649 }
1650 }
1651 auto pq = priority_queue<status>();
1652 pq.push(status(1, 0));
1653 bool flag = false;
1654 int prev = 0;
1655 while(!pq.empty()) {
1656 status current = pq.top();
1657 pq.pop();
1658 if(current.position == n) {
1659 if(!flag) {
1660 flag = true;
1661 prev = current.time;
1662 } else {
1663 if(current.time != prev) {
1664 return current.time;
1665 }
1666 }
1667 }
1668
1669 if(record[current.position].empty() || record[current.position].size() == 1 && current.time != *record[current.position].rbegin()) {
1670
1671 record[current.position].push_back(current.time);
1672 } else {
1673 continue;
1674 }
1675
1676 for(int next: um[current.position]) {
1677 int next_time;
1678 if(current.time / change % 2 == 1) {
1679
1680 next_time = (current.time / change + 1) * change + time;
1681 } else {
1682 next_time = current.time + time;
1683 }
1684 auto next_status = status(next, next_time);
1685 if(record[next_status.position].empty() || record[next_status.position].size() == 1 && next_status.time != *record[next_status.position].rbegin()) {
1686
1687 pq.push(next_status);
1688 }
1689 }
1690 }
1691 return 0;
1692 }