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;
const int N = 100010;
class DisjointSet {
 public:
  int rank[N], p[N];
  void make_set(int x) {
    p[x] = x;
    rank[x] = 0;
  }
  void merge(int x, int y) { link(find_set(x), find_set(y)); }
  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[y] = x;
      if (rank[x] == rank[y]) rank[y]++;
    }
  }
  int find_set(int x) {
    if (x != p[x]) p[x] = find_set(p[x]);
    return p[x];
  }
};
class st {
 public:
  int now;
  long long cost;
  int num;
  bool operator<(const st &a) const {
    if (cost != a.cost) return cost > a.cost;
    return num > a.num;
  }
};
bool cmp(long long acost, int anum, long long bcost, int bnum) {
  if (acost != bcost) return acost < bcost;
  return anum < bnum;
}
int getindex(string &in, map<string, int> &M) {
  int index = M.size();
  if (M.find(in) == M.end()) M[in] = index;
  return M[in];
}
vector<st> edge[N];
pair<long long, int> cost[N];
int stname[N], lname[N];
bool vis[N];
bool isconnect(int n, vector<int> &s, int d, DisjointSet &dj) {
  for (int i = 0; i < s.size(); i++) {
    if (dj.find_set(d) == dj.find_set(stname[s[i]])) return true;
  }
  return false;
}
const int QSIZE = 2001;
class PQ {
 public:
  int qsize, top;
  PQ() { qsize = 0, top = 0; }
  queue<st> Q[QSIZE];
  void push(st a, int adv) {
    Q[(top + adv) % QSIZE].push(a);
    qsize++;
  }
  st pop() {
    while (Q[top].empty()) top = (top + 1) % QSIZE;
    st ret = Q[top].front();
    Q[top].pop();
    qsize--;
    return ret;
  }
  bool empty() { return qsize == 0; }
};
const long long inf = (1LL << 60);
const int intinf = (1 << 25);
int cnt = 0;
void dijkstra(int n, vector<int> &s, int d, int T) {
  for (int i = 0; i < n; i++) cost[i].first = inf, cost[i].second = intinf;
  PQ Q;
  for (int i = 0; i < s.size(); i++) {
    cost[s[i]].first = 0;
    cost[s[i]].second = 0;
    Q.push((st){s[i], 0, 0}, 0);
  }
  long long ans = inf;
  int nans = 0;
  while (!Q.empty()) {
    st now = Q.pop();
    int ns = stname[now.now], nl = lname[now.now];
    if (cost[now.now].first != now.cost || cost[now.now].second != now.num) {
      continue;
    }
    cnt++;
    if (now.cost > ans) break;
    if (stname[now.now] == d) {
      if (now.cost < ans || (now.cost == ans && now.num < nans))
        ans = now.cost, nans = now.num;
      continue;
    }
    for (int i = 0; i < edge[ns].size(); i++) {
      int next = edge[ns][i].now;
      int nel = lname[edge[ns][i].now];
      long long nec = now.cost + edge[ns][i].cost;
      int nenum = now.num;
      if (abs(now.now - next) != 1 || nel != nl) nec += T, nenum++;
      long long dif = nec - now.cost;
      if ((nec < cost[next].first) ||
          (nec == cost[next].first && nenum < cost[next].second)) {
        cost[next].first = nec;
        cost[next].second = nenum;
        if (nec <= ans) Q.push((st){next, nec, nenum}, (int)dif);
      }
    }
  }
  if (ans != inf)
    cout << ans << " " << nans << endl;
  else
    cout << -1 << endl;
}
vector<st> tmpedge[N];
DisjointSet dj;
int hoge[N], hoge2[N];
void makegraph(int *s, int *cost, int lnum, int base, int p) {
  for (int i = 0; i < (int)p - 1; i++) {
    edge[s[i]].push_back((st){base + i + 1, cost[i], lnum});
    edge[s[i + 1]].push_back((st){base + i, cost[i], lnum});
    dj.merge(stname[base + i], stname[base + i + 1]);
  }
}
string getname() {
  string ret = "";
  char c;
  while (true) {
    c = getchar();
    if (isalpha(c)) {
      ret += c;
      break;
    }
  }
  while (true) {
    c = getchar();
    if (isalpha(c))
      ret += c;
    else
      break;
  }
  return ret;
}
int main(int argc, char *const argv[]) {
  for (int i = 0; i < N; i++) dj.make_set(i);
  map<string, int> M;
  int n, t;
  string fname, dname;
  scanf("%d%d", &n, &t);
  fname = getname();
  dname = getname();
  getindex(fname, M);
  getindex(dname, M);
  vector<int> start;
  int total = 0;
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    int prev;
    int p = k;
    for (int j = 0; j < k; j++) {
      string name;
      name = getname();
      if (name == fname) start.push_back(total + j);
      hoge[j] = (getindex(name, M));
      stname[total + j] = hoge[j];
      lname[total + j] = i;
    }
    for (int j = 0; j < k - 1; j++) scanf("%d", &hoge2[j]);
    makegraph(hoge, hoge2, i, total, k);
    total += k;
  }
  if (!isconnect(total, start, getindex(dname, M), dj)) {
    cout << -1 << endl;
    return 0;
  }
  dijkstra(total, start, getindex(dname, M), t);
  return 0;
}
