Example

Input

2 10
Warsaw Petersburg
3
Kiev Moscow Petersburg
150 120
3
Moscow Minsk Warsaw
100 150


Output

380 1
#include <bits/stdc++.h>
using namespace std;
int pick_name(const string &s, map<string, int> &name_table) {
  map<string, int>::iterator it = name_table.find(s);
  if (it != name_table.end()) {
    return it->second;
  }
  int index = name_table.size();
  name_table.insert(make_pair(s, index));
  return index;
}
int main() {
  ios_base::sync_with_stdio(false);
  int N, T;
  cin >> N >> T;
  vector<vector<int> > platforms;
  vector<vector<pair<int, int> > > conn;
  vector<int> reverse_lookup;
  map<string, int> name_table;
  string A, B;
  cin >> A >> B;
  int start = pick_name(A, name_table);
  int goal = pick_name(B, name_table);
  while (N--) {
    int a;
    cin >> a;
    vector<int> stations(a), costs(a - 1);
    for (int i = 0; i < a; ++i) {
      string s;
      cin >> s;
      stations[i] = pick_name(s, name_table);
    }
    for (int i = 0; i + 1 < a; ++i) {
      cin >> costs[i];
    }
    int prev = conn.size();
    conn.push_back(vector<pair<int, int> >());
    if (stations[0] >= platforms.size()) {
      platforms.resize(stations[0] + 1);
    }
    platforms[stations[0]].push_back(prev);
    reverse_lookup.push_back(stations[0]);
    for (int i = 1; i < a; ++i) {
      int cur = conn.size();
      conn.back().push_back(pair<int, int>(cur, costs[i - 1]));
      conn.push_back(
          vector<pair<int, int> >(1, pair<int, int>(prev, costs[i - 1])));
      if (stations[i] >= platforms.size()) {
        platforms.resize(stations[i] + 1);
      }
      platforms[stations[i]].push_back(cur);
      reverse_lookup.push_back(stations[i]);
      prev = cur;
    }
  }
  for (int i = 0; i < platforms.size(); ++i) {
    sort(platforms[i].begin(), platforms[i].end());
  }
  vector<bool> passed(conn.size());
  priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> >,
                 greater<pair<pair<int, int>, int> > >
      pq;
  for (int i = 0; i < platforms[start].size(); ++i) {
    pq.push(
        pair<pair<int, int>, int>(pair<int, int>(0, 0), platforms[start][i]));
  }
  int time = -1, transit = 0;
  while (!pq.empty()) {
    pair<pair<int, int>, int> pp = pq.top();
    pq.pop();
    int a = pp.first.first, t = pp.first.second, v = pp.second;
    if (passed[v]) {
      continue;
    }
    passed[v] = true;
    int s = reverse_lookup[v];
    if (s == goal) {
      time = a;
      transit = t;
      break;
    }
    for (int i = 0; i < platforms[s].size(); ++i) {
      if (passed[platforms[s][i]]) {
        continue;
      }
      pq.push(pair<pair<int, int>, int>(pair<int, int>(a + T, t + 1),
                                        platforms[s][i]));
    }
    for (int i = 0; i < conn[v].size(); ++i) {
      if (passed[conn[v][i].first]) {
        continue;
      }
      pq.push(pair<pair<int, int>, int>(
          pair<int, int>(a + conn[v][i].second, t), conn[v][i].first));
    }
  }
  if (time < 0) {
    cout << -1 << endl;
  } else {
    cout << time << " " << transit << endl;
  }
  return 0;
}
