#include<iostream>
#include<string>
#include<algorithm>
#include<complex>
#include<vector>
#include<map>
using namespace std;

/*struct anime{
  string name;
  int weekday;
  int start;
  long int minutes;
  bool favorite;
  void setup(string n,int w,int s){
    name = n;
    weekday = w;
    start = s;
    favorite = false;
    int h = start / 100;
    int m = start % 100;
    if(h >= 24){
      h -= 24;
      w++;
    }
    minutes = h*60 + m + weekday * 60 * 24;
  }
};*/

int setup(int w,int s){
  int h = s / 100;
  int m = s % 100;
  if(h >= 24){
    h -= 24;
    w++;
  }
  return h*60 + m + w * 60 * 24;

}
/*
bool operator<(anime a1,anime a2){
  return a1.minutes < a2.minutes;
}*/

int main(){

    int n,f,we,st,count;
    string na;

    while(1){
      cin >> n;
      if(n == 0){
        break;
      }

      vector<int> table;
      vector<int> favoritetable;
      map<string,int> amap;

      for(int i=0; i<n; i++){
        cin >> na >> we >> st;
        int t;
        t = setup(we,st);
        table.push_back(t);
        amap[na] = t;
      }

      cin >> f;

      for(int i=0; i<f; i++){
        cin >> na;
        favoritetable.push_back(amap[na]);
      }

      sort(table.begin() , table.end());
      sort(favoritetable.begin(), favoritetable.end());

    //  int first=0,last=1,beforelast;
      bool flag,cansee = true,changelast;
      count = 0;

      for(int i=1; i<favoritetable.size(); ++i){
        if(abs(favoritetable[i] - favoritetable[i-1]) < 30){
          cansee = false;
        }
      }

      for(int i=0; i<table.size(); i++){
        for(int j=0; j<favoritetable.size(); j++){
          if(abs(table[i] - favoritetable[j]) < 30){
            table.erase(table.begin() + i);
            i--;
            break;
          }
        }
      }

      int ans = table.size() + f;
      long int tmp = 0;

      for(int i=0; i<table.size(); i++){
        if(abs(table[i] - tmp) < 30){
          ans--;
        }
        else{
          tmp = table[i];
        }
      }

      if(!cansee){
        cout << -1 << endl;
      }
      else{
        cout << ans << endl;
      }

      /*

      while(last != n){

        while(1){
          if(abs(table[last].minutes - table[first].minutes) >= 30){
              break;
          }
          last++;
          if(last == n){
            last--;
            break;
          }
        }

        beforelast = last;

        flag = false;

        changelast = false;

        for(int i=first; i<last; i++){
          if(!flag && table[i].favorite){
            flag = true;
            if(i != first){
              last = i;
              changelast = true;
            }
          }
          else if(flag && table[i].favorite){
            cansee = false;
            break;
          }
        }

        if(!cansee){
          break;
        }

        if(!flag || (flag && !changelast) ){//???????????????????????£??????????????????????????´???????????????count++????????????
          count++;
        }

        first = last;
        last++;

      }

      if(abs(table[n-1].minutes - table[n-2].minutes) >= 30){
        count++;
      }



      if(!cansee){
        cout << -1 << endl;
      }
      else{
        cout << count << endl;
      }

      */

    }


    return 0;
}

Measured memory_bytes: 3092