#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ss string
#define db double
#define endl '\n'
#define pi 3.1415927
#define outd fixed << setprecision(6)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
const int N=510;
int main() {
IOS
vector<int> frq(N);
ss s;
int cnt=0;
while(getline(cin,s)) {
for(int i=0;i<s.size();i++) {
if(s[i]=='{') {
cnt++;
}
else if(s[i]=='}') {
cnt--;
}
else if(s[i]=='p') {
frq[cnt]++;
}
}
}
vector<pair<int,int> > v;
for(int i=1;i<510;i++) {
if(frq[i]!=0) {
v.push_back({i,frq[i]});
}
}
for(int i=0;i<v.size();i++) {
if(i) {
cout<<" + ";
}
if(v[i].second>1) {
cout<<v[i].second;
}
cout<<"N";
if(v[i].first>1) {
cout<<"^" << v[i].first;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgSU9TICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApO2Npbi50aWUoMCk7Y291dC50aWUoMCk7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgc3Mgc3RyaW5nCiNkZWZpbmUgZGIgZG91YmxlCiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgcGkgMy4xNDE1OTI3CiNkZWZpbmUgb3V0ZCBmaXhlZCA8PCAgc2V0cHJlY2lzaW9uKDYpCiNkZWZpbmUgYWxsKHgpICh4KS5iZWdpbigpLCAoeCkuZW5kKCkKI2RlZmluZSByYWxsKHgpICh4KS5yYmVnaW4oKSwgKHgpLnJlbmQoKQp1c2luZyBuYW1lc3BhY2UgIHN0ZDsKY29uc3QgaW50IE49NTEwOwppbnQgbWFpbigpIHsKICAgIElPUwogICAgdmVjdG9yPGludD4gZnJxKE4pOwogICAgc3MgczsKICAgIGludCBjbnQ9MDsKICAgIHdoaWxlKGdldGxpbmUoY2luLHMpKSB7CiAgICAgICAgZm9yKGludCBpPTA7aTxzLnNpemUoKTtpKyspIHsKICAgICAgICAgICAgaWYoc1tpXT09J3snKSB7CiAgICAgICAgICAgICAgICBjbnQrKzsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKHNbaV09PSd9JykgewogICAgICAgICAgICAgICAgY250LS07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZihzW2ldPT0ncCcpIHsKICAgICAgICAgICAgICAgIGZycVtjbnRdKys7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICB2ZWN0b3I8cGFpcjxpbnQsaW50PiA+IHY7CiAgICBmb3IoaW50IGk9MTtpPDUxMDtpKyspIHsKICAgICAgICBpZihmcnFbaV0hPTApIHsKICAgICAgICAgICAgdi5wdXNoX2JhY2soe2ksZnJxW2ldfSk7CiAgICAgICAgfQogICAgfQogICAgZm9yKGludCBpPTA7aTx2LnNpemUoKTtpKyspIHsKICAgICAgICBpZihpKSB7CiAgICAgICAgICAgIGNvdXQ8PCIgKyAiOwogICAgICAgIH0KICAgICAgICBpZih2W2ldLnNlY29uZD4xKSB7CiAgICAgICAgICAgIGNvdXQ8PHZbaV0uc2Vjb25kOwogICAgICAgIH0KICAgICAgICAgICAgY291dDw8Ik4iOwogICAgICAgIGlmKHZbaV0uZmlyc3Q+MSkgewogICAgICAgICAgICBjb3V0PDwiXiIgPDwgdltpXS5maXJzdDsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQoK