/*
 *	IEEEXtreme 2008
 *	Problem 1: Merging IP ranges
 *	Ertuğ Karamatlı (http://www.ertugkaramatli.com)
 *	Doğuş Üniversitesi IEEE Student Branch
 *
 */

#include <cstdlib>
#include <cstring>
#include <string>
#include <fstream>
#include <iostream>
#include <math.h>

using namespace std;

const int MAX_IP_COUNT=350000;

int ip2int(string ip) {
	char * cstr;
	cstr = new char [ip.size()+1];
	strcpy (cstr, ip.c_str());
	
	int power=3;
	int result=0;
	
	char *pch = strtok(cstr,".");
	
	while (pch != NULL)
	{
		int seg=atoi(pch);
		
		int temp=(int)pow(256,power);
		
		result+=seg*temp;

		pch = strtok(NULL, ".");
		
		power--;
	}
	
	return result;
}



int main(int argc, char *argv[])
{
	
	
	string line;
	//ifstream myfile ("ipler.txt");
	
	string type;
	char *pch;

	int line_count=1;
	int ip_count=0;
	
	string ips[MAX_IP_COUNT][2];
	
	do
	{
		getline (cin,line);

		if (line_count==1)
			type=line;
		else {
			//parse ip
			char * cstr;

		  cstr = new char [line.size()+1];
			  strcpy (cstr, line.c_str());
			
			unsigned int ip;

			
			pch = strtok (cstr,"-");
			
			while (pch != NULL)
			{
				int lc=line_count-2;
				int ic=ip_count%2;
				ips[lc][ic]=pch;
				pch = strtok (NULL, "-");
				
				ip_count++;
			}
			
		}
		
		
		line_count++;
	}
	while (line!="");

	
	for (int i=0;i < ip_count/2;i++) {
		for (int j=0;j < ip_count/2;j++) {
			bool found=false;
			
			if (i!=j) {
				if (ip2int(ips[i][0]) > ip2int(ips[j][0]) && ip2int(ips[i][0]) < ip2int(ips[j][1])) { // lower bound
					
					ips[i][0]=ips[j][0];
					found=true;
									
				}
				
				if (ip2int(ips[i][1]) > ip2int(ips[j][0]) && ip2int(ips[i][1]) < ip2int(ips[j][1])) { // upper bound
					
					ips[i][1]=ips[j][1];
					found=true;
					
				}
				
				if (ip2int(ips[i][0]) == ip2int(ips[j][1])+1) { // upper bound
					
					ips[i][0]=ips[j][0];
					ips[j][0]="NULL";
					ips[j][1]="NULL";
					
				}
				
				
				if (found) {
					ips[j][0]="NULL";
					ips[j][1]="NULL";
				}
				
				
			}
		}
	}

  int i, j;
  string test, temp1,temp2;
  for(i = ip_count/2 - 1; i > 0; i--)
  {
	test="";
	for(j = 0; j < i; j++)
	{
	  if(ip2int(ips[j][0]) > ip2int(ips[j+1][0]))
	  {
		temp1 = ips[j][0]; 
		temp2 = ips[j][1]; 
		ips[j][0] = ips[j+1][0];
		ips[j][1] = ips[j+1][1];
		ips[j+1][0] = temp1;
		ips[j+1][1] = temp2;
		
		test=1;
	  }
	}
	if(test=="") break;
  }
	
	
	if (type=="S") {
		for (int i=0;i < ip_count/2;i++) {
			if (ips[i][0]!="NULL") {
				cout<<ips[i][0]<<"-"<<ips[i][1]<<endl;
			}
		}
	}
	else {
		int range_sum=0;
		for (int i=0;i < ip_count/2;i++) {
			if (ips[i][0]!="NULL") {
				range_sum+=ip2int(ips[i][1])-ip2int(ips[i][0]);
			}
		}
		cout<<range_sum;
	}

	return 0;
}
